[英]How to refresh a dictionary in a tkinter mainloop() in Python?
我是编程新手,我一直在尝试了解python / tkinter何时刷新字典值。
我有以下代码,该代码基本上创建了一个称为状态的字典,其中该项目链接到值为0。
然后,我启动GUI。
from Tkinter import *
import Tkinter, tkFileDialog
states = {'open_thermo':0}
def is_true(state): # in quotes
states[state] = 1
def Station():
Station = Tk()
if states['open_thermo'] == True:
Label(master=Station, text='Thermo has been opened').grid(row=0)
Button(master=Station, text='press to end', command= lambda: combine_funcs(Station.destroy())).grid(row=4, columnspan=1, column=1, pady=4)
else:
Label(master=Station, text='Do you wish to open Thermo ?').grid(row=0)
Button(master=Station, text='press to open Thermo', command= lambda: is_true('open_thermo')).grid(row=4, columnspan=1, column=1, pady=4)
print states['open_thermo'] == True #1st print statement
mainloop()
start = Tk()
Label(master=start, text='start').grid(row=0)
Button(master=start, text='press to start', command= lambda: combine_funcs(start.destroy(),Station())).grid(row=4, columnspan=1, column=1, pady=4)
mainloop()
print states['open_thermo'] == True #2nd print statement
我不明白两件事:
print
语句被注释为#1st print statement
不会一遍又一遍地打印。 虽然我mainloop()
重复了一遍以上的代码。 为什么,当我单击按钮时, states['open_thermo']
的值不更改为1,然后if states['open_thermo'] == True:
由于mainloop()
语句变为true。 我希望标签更改为:
Label(master=Station, text='Thermo has been opened').grid(row=0) Button(master=Station, text='press to end', command= lambda: combine_funcs(Station.destroy())).grid(row=4, columnspan=1, column=1, pady=4)
预先感谢您的帮助 !
我是编程新手,我一直在尝试了解python / tkinter何时刷新字典值。
python / tkinter从不“刷新字典的值”。 当您告诉他们更改字典时,它们会立即更改。
- 为什么在单击按钮之前,打印语句被注释为#1st打印语句而不会一遍又一遍地打印。 虽然我mainloop()重复了一遍以上的代码。
不, mainloop
不重复代码,它上面。 上面的代码只运行一次。 mainloop()
是一个简单的循环,它等待事件并根据绑定对事件做出响应。 在mainloop
之后,直到销毁窗口之前,都不会运行任何代码。
- 为什么当我单击按钮时,states ['open_thermo']的值不更改为1,然后if status ['open_thermo'] == True:由于mainloop(),语句变为true。
目前尚不清楚您正在谈论哪个按钮。 但是,看起来两个调用is_true
按钮实际上都会更改该值。 我看不到任何证据都没有。 由于is_true
除了更改值不会执行任何操作,因此我看不到您认为它没有更改。
同样, Station
的代码在被调用时仅运行一次。 它不会一遍又一遍地运行。 您单击一次按钮,它将被调用一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.