Tkinter使用按钮和方法动态更改标签

时间:2016-07-06 12:36:04

标签: python user-interface tkinter

我正在使用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()   

目前我可以设置一次消息,或者将其留空,但不确定我缺少什么来动态更改它。 感谢

1 个答案:

答案 0 :(得分:1)

首先,您必须在v创建 makeWindow,然后将其设为global

def makeWindow() : 
    global  v
    win = Tk()
    v = StringVar()

然后,将其用作文本字段的textvariable

    msg = Message(frame1, textvariable = v) 

最后,在global中使用updateEntry访问现有变量:

def updateEntry(): 
    global v
    v.set("additinalText")