首页 文章

变量更改时,让tkinter标签更新

提问于
浏览
0

我正在尝试用pythons tkinter模块构建一个应用程序 .

目前,我正在尝试在选择单选按钮时更改标签显示文本 . 我将标签文本设置为textvariable,并根据所选的按钮将textvariable更改为所需的文本 . 但是,我期待标签文本发生变化,因为它的管理文本变量已经改变 . 但是不会更新 .

任何有关更新它的帮助将非常感谢谢谢 .

info = Label(mainwindow, bg = 'magenta', height = 10, width = 40, text = weatherinfo, font = ('arial', 14, 'normal'))
info.pack(side = LEFT,padx = 20)

weatherinfo = 'select your city'

然后我的检查功能改变了weatherinfo

weatherinfo = '\n'.join([z, y, x, w, v, u,t])
print weatherinfo

在shell上打印正确的值,但原始信息标签不会更新并仍然显示“选择您的城市”

1 回答

  • 0

    在此代码中, Label 使用 RadiobuttonLabel 的文本变量的命令选项更新 Radiobutton 的每次单击 .

    from Tkinter import *
    
    def radio_action():
        value1 = var1.get()
        changeable_label['text'] = value1
    
    the_window = Tk()
    the_window.title('Example')
    
    the_window.geometry("200x150")
    
    var1 = StringVar()
    
    changeable_label = Label(the_window, text = "Null")
    button1 = Radiobutton(the_window, text = 'Button1', variable = var1,
                         value="Something", command = radio_action)
    
    changeable_label.pack(side = TOP)
    button1.pack()
    
    
    the_window.mainloop()
    

    由于我不知道你的整个代码,这是我可以帮助一个例子 .

相关问题