tkinter库将按钮运行结果显示在文本框中并且自动清除
以下是一个示例代码,其中使用了一个文本框和一个按钮,当点击按钮时,它将在文本框中显示一条消息,并自动清除文本框:
import tkinter as tk
def clear_text():
text.delete('1.0', 'end')
def show_message():
message = "Hello, World!"
text.insert('end', message + '\n')
root = tk.Tk()
text = tk.Text(root)
text.pack()
button = tk.Button(root, text="Show Message", command=show_message)
button.pack()
clear_button = tk.Button(root, text="Clear Text", command=clear_text)
clear_button.pack()
root.mainloop()
在这个例子中,我们定义了两个函数: show_message() 和 clear_text()。 show_message() 函数在文本框中插入一条消息,并在消息后添加一个换行符。 clear_text() 函数将删除文本框中的所有文本。
我们创建一个 Text 对象,用于显示文本,并将其添加到根窗口中。我们还创建了一个 Button 对象,用于触发显示消息的函数。我们还创建了另一个 Button 对象,用于触发清除文本的函数。
最后,我们调用 mainloop() 方法启动 GUI 循环,以便用户可以与应用程序交互
原文地址: https://www.cveoy.top/t/topic/eD3D 著作权归作者所有。请勿转载和采集!