2016-07-06 161 views
2

我正在使用Tkinter在GUI上工作。我正在嘗試動態更新消息框的內容。下面是我的代碼Tkinter使用按鈕和方法動態更改標籤

高清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

首先,你必須創建makeWindowv,使之global

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

然後,把它作爲textvariable文本字段:

msg = Message(frame1, textvariable = v) 

最後,訪問現有的變量globalupdateEntry

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