更改条目时 Python Tkinter 更新 [英] Python Tkinter update when entry is changed
问题描述
我正在尝试用 python 制作一个简单的温度转换计算器.我想要做的是能够输入一个数字,让对方自动更新,而无需按下按钮.现在我只能让它朝着一个方向工作.我可以对它进行编码,以便它可以从 F 到 C,或从 C 到 F.但不是任何一种方式.
I'm trying to make a simple temperature conversion calculator in python. What I want to do is to be able to type in a number, and have the other side automatically update, without having to push a button. Right now I can only get it to work in one direction. I can either code it so that it can go from F to C, or C to F. But not either way.
显然 after
不是要走的路.我需要某种 onUpdate
之类的东西.TIA!
Obviously after
is not the way to go. I need some kind of onUpdate
or something. TIA!
import Tkinter as tk
root = tk.Tk()
temp_f_number = tk.DoubleVar()
temp_c_number = tk.DoubleVar()
tk.Label(root, text="F").grid(row=0, column=0)
tk.Label(root, text="C").grid(row=0, column=1)
temp_f = tk.Entry(root, textvariable=temp_f_number)
temp_c = tk.Entry(root, textvariable=temp_c_number)
temp_f.grid(row=1, column=0)
temp_c.grid(row=1, column=1)
def update():
temp_f_float = float(temp_f.get())
temp_c_float = float(temp_c.get())
new_temp_c = round((temp_f_float - 32) * (5 / float(9)), 2)
new_temp_f = round((temp_c_float * (9 / float(5)) + 32), 2)
temp_c.delete(0, tk.END)
temp_c.insert(0, new_temp_c)
temp_f.delete(0, tk.END)
temp_f.insert(0, new_temp_f)
root.after(2000, update)
root.after(1, update)
root.mainloop()
推荐答案
您正在寻找的是变量 trace() 方法.例如:
What you are looking for is variable trace() method. E.g.:
def callback(*args):
print "variable changed!"
var = DoubleVar()
var.trace("w", callback)
为您的每个 DoubleVar 附加跟踪回调,以便 temp_f_number 一更新 temp_c_number 值,反之亦然.您可能还需要在另一个回调函数中禁用一个回调函数,以避免递归更新周期.
Attach trace callbacks for each of your DoubleVar, for temp_f_number one to update the temp_c_number value and vice versa. You'll likely also need to disable one callback function while inside another one, to avoid recursive update cycle.
另一个注意事项 - 不要编辑输入字段.相反,使用变量的 set() 方法.输入字段将自动更新.
Another note - do not edit the Entry fields. Instead, use variables' set() method. Entry fields will be updated automatically.
所以,完整的代码可能如下所示:
So, complete code could look like this:
import Tkinter as tk
root = tk.Tk()
temp_f_number = tk.DoubleVar()
temp_c_number = tk.DoubleVar()
tk.Label(root, text="F").grid(row=0, column=0)
tk.Label(root, text="C").grid(row=0, column=1)
temp_f = tk.Entry(root, textvariable=temp_f_number)
temp_c = tk.Entry(root, textvariable=temp_c_number)
temp_f.grid(row=1, column=0)
temp_c.grid(row=1, column=1)
update_in_progress = False
def update_c(*args):
global update_in_progress
if update_in_progress: return
try:
temp_f_float = temp_f_number.get()
except ValueError:
return
new_temp_c = round((temp_f_float - 32) * 5 / 9, 2)
update_in_progress = True
temp_c_number.set(new_temp_c)
update_in_progress = False
def update_f(*args):
global update_in_progress
if update_in_progress: return
try:
temp_c_float = temp_c_number.get()
except ValueError:
return
new_temp_f = round(temp_c_float * 9 / 5 + 32, 2)
update_in_progress = True
temp_f_number.set(new_temp_f)
update_in_progress = False
temp_f_number.trace("w", update_c)
temp_c_number.trace("w", update_f)
root.mainloop()
这篇关于更改条目时 Python Tkinter 更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!