我正在使用Tkinter开发GUI。我正在尝试动态更新消息框的内容。下面是我的代码
def makeDisplay():
def updateEntry():
#phonelist[0]
v = StringVar()
v.set("additinalText")
def makeWindow() :
global v
win = Tk()
frame1 = Frame(win)
frame1.pack()
Label(frame1, text= "text argument here").grid(row = 0, column = 0, sticky = W)
msg = Message(frame1, textvariable = updateEntry)
msg.config(bg = "lightgreen", font = ('times', 24, 'italic'))
msg.grid(row = 1, column = 0, sticky= W)
frame2 = Frame(win)
frame2.pack()
b1 = Button(frame2, text= " Update ", command = updateEntry)
b1.pack(side = LEFT)
return win
win = makeWindow()
win.mainloop()
makeDisplay()
目前,我可以将消息设置一次,也可以将其留空,但不确定我缺少什么来动态更改它。谢谢
首先,您必须在 makeWindow
中创建 v
,并使其global
:
def makeWindow() :
global v
win = Tk()
v = StringVar()
然后,将其用作文本字段的textvariable
:
msg = Message(frame1, textvariable = v)
最后,使用 global
在 updateEntry
中访问现有变量:
def updateEntry():
global v
v.set("additinalText")