我的问题是关于使用tkinter在python中进行GUI编程。我相信这是python3x
我的问题是:当我们执行一个程序来运行GUI时,一个按钮可以更新一个标签吗?更具体地说,有没有办法在按下按钮后更改标签显示的文本?我以前曾咨询过stack overflow,并采用了StringVar()方法,但它似乎并没有解决我的问题,实际上它完全忽略了GUI中的文本!在
下面是代码
from tkinter import *
root = Tk()
root.title('Copy Text GUI Program')
copiedtext = StringVar()
copiedtext.set("Text is displayed here")
def copytext():
copiedtext.set(textentered.get())
# Write 'Enter Text Here'
entertextLabel = Label(root, text="Enter Text Here")
entertextLabel.grid(row=0, column=0)
# For the user to write text into the gui
textentered = Entry(root)
textentered.grid(row=0, column=1)
# The Copy Text Button
copytextButton = Button(root, text="Copy Text")
copytextButton.grid(row=1, columnspan=2)
# Display the copied text
displaytextLabel = Label(root, textvariable=copiedtext)
displaytextLabel.grid(row=2,columnspan=2)
copytextButton.configure(command=copytext())
root.mainloop()
任何帮助都将不胜感激!在
您需要做的是将按钮事件绑定到copytextButton对象,如下所示:
这意味着左键单击按钮时将调用回调函数-copytext()。在
函数本身需要稍作修改,因为回调会发送一个事件参数:
^{pr2}$编辑: 不需要此行:
相关问题 更多 >
编程相关推荐