在变量更改时获取 tkinter 标签更新 [英] Getting tkinter labels to update when variable changes

查看:20
本文介绍了在变量更改时获取 tkinter 标签更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to build an app with pythons tkinter module.

目前我正在尝试在选择单选按钮时更改标签显示的文本.我将标签文本设置为文本变量,以及根据选择的按钮将文本变量更改为所需文本的函数.但是,我希望 Labels 文本会发生变化,因为它管理的 textvariable 已经发生了变化.但是没有更新.

Currently I'm trying to get a labels displayed text to change when a radiobutton is selected. I have the labels text set to a textvariable, and a function which changes the textvariable to the required text depending on which button is selected. However, I'm expecting the Labels text to change since it's governing textvariable has changed. However is doesn't update.

任何帮助更新它的帮助将不胜感激.

Any help to get it updating will be greatly appreciated thanks.

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

Then my check function changes weatherinfo

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

在外壳上打印出正确的值,但原始信息标签不会更新并保持显示选择您的城市"

The correct values print out on the shell but the original info label doesnt update and remains displaying 'select your city'

推荐答案

在这段代码中,Label 使用 Radiobutton<的命令选项更新每次点击 Radiobutton/code> 和 Label 的文本变量.

In this code, Label updates every click of Radiobutton using command option of Radiobutton and Label's text variable.

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()

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

Since I don't know your whole code, this is I can help with an example.

这篇关于在变量更改时获取 tkinter 标签更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆