【发布时间】:2019-04-27 11:52:47
【问题描述】:
我正在尝试创建一个自动显示输入变量结果的标签。 基本上我正在尝试将这两个程序结合起来:
from tkinter import *
root = Tk()
var = StringVar()
var.set('hello')
l = Label(root, textvariable = var)
l.pack()
t = Entry(root, textvariable = var)
t.pack()
root.mainloop() # the window is now displayed
这个(来源:Update Tkinter Label from variable)自动更新标签,但它只能更新为用户输入的内容。
还有这个:
from tkinter import *
myWindow = Tk()
def MyCalculateFunction():
pressure, temprature = float(box_pressure.get()), float(box_temprature.get())
result = pressure + temperature
label_result.config(text="%f + %f = %f" % (pressure, temprature, result))
box_pressure = Entry(myWindow)
box_pressure.pack()
box_temprature = Entry(myWindow)
box_temprature.pack()
button_calculate = Button(myWindow, text="Calcuate", command=MyCalculateFunction)
button_calculate.pack()
label_result = Label(myWindow)
label_result.pack()
我遇到的问题是,如果用户改变压力或温度,结果不会自动改变。 (来源:How to get value from entry (Tkinter), use it in formula and print the result it in label)
我怎样才能使当用户更改任何变量时,Python 会自动计算新结果并自行更改标签?
【问题讨论】:
-
将
label_result.config(...移动到def MyCalculateFunction():内
标签: python variables tkinter label