我试图创建一个标签,自动显示输入变量的结果。 基本上我试着把这两个程序结合起来:
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自动计算新结果并自行更改标签?你知道吗
只是你错过了几件事。你知道吗
Tkinters小部件需要变量来保存输入到它们中的值,而在温度和压力小部件中创建这些值时却忽略了这一点。
最好先计算值,然后
set
widgets变量。希望这有帮助。你知道吗
相关问题 更多 >
编程相关推荐