简体   繁体   English

如何在Python的tkinter mainloop()中刷新字典?

[英]How to refresh a dictionary in a tkinter mainloop() in Python?

I am new to programming and I have been trying to understand when python/tkinter refresh values of dictionaries. 我是编程新手,我一直在尝试了解python / tkinter何时刷新字典值。

I've got the following code which basically creates a dictionary called states in which the item is linked to a value of 0. 我有以下代码,该代码基本上创建了一个称为状态的字典,其中该项目链接到值为0。

I then launch the GUI. 然后,我启动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

I do not understand two things: 我不明白两件事:

  1. why the print statement commented as #1st print statement doesn't print over and over again before I click the button. 为什么在单击按钮之前, print语句被注释为#1st print statement不会一遍又一遍地打印。 I though mainloop() repeated the code above it over and over again. 虽然我mainloop()重复了一遍以上的代码。
  2. why, when I click the button, the value of states['open_thermo'] doesn't change to 1, and then the if states['open_thermo'] == True: statement becomes true because of mainloop() . 为什么,当我单击按钮时, states['open_thermo']的值不更改为1,然后if states['open_thermo'] == True:由于mainloop()语句变为true。 I would expect the label to change to: 我希望标签更改为:

     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) 

Thank you in advance for your help ! 预先感谢您的帮助 !

I am new to programming and I have been trying to understand when python/tkinter refresh values of dictionaries. 我是编程新手,我一直在尝试了解python / tkinter何时刷新字典值。

python/tkinter never "refreshes values of dictionaries". python / tkinter从不“刷新字典的值”。 The dictionaries change immediately when you tell them to change. 当您告诉他们更改字典时,它们会立即更改。

  1. why the print statement commented as #1st print statement doesn't print over and over again before I click the button. 为什么在单击按钮之前,打印语句被注释为#1st打印语​​句而不会一遍又一遍地打印。 I though mainloop() repeated the code above it over and over again. 虽然我mainloop()重复了一遍以上的代码。

No, mainloop does not repeat code that is above it. 不, mainloop 重复代码,它上面。 The code above it runs exactly once. 上面的代码只运行一次。 mainloop() is a simple loop that waits for events and responds to them based on bindings. mainloop()是一个简单的循环,它等待事件并根据绑定对事件做出响应。 No code will run after mainloop until the window is destroyed. mainloop之后,直到销毁窗口之前,都不会运行任何代码。

  1. why, when I click the button, the value of states['open_thermo'] doesn't change to 1, and then the if states['open_thermo'] == True: statement becomes true because of mainloop(). 为什么当我单击按钮时,states ['open_thermo']的值不更改为1,然后if status ['open_thermo'] == True:由于mainloop(),语句变为true。

It's unclear which button you're talking about. 目前尚不清楚您正在谈论哪个按钮。 However, it looks like both buttons that call is_true will actually change the value. 但是,看起来两个调用is_true按钮实际上都会更改该值。 I don't see any evidence that it doesn't. 我看不到任何证据都没有。 Since is_true doesn't do anything except change the value, I don't see how you think it's not changing. 由于is_true除了更改值不会执行任何操作,因此我看不到您认为它没有更改。

Again, the code in Station runs exactly once when it is called. 同样, Station的代码在被调用时仅运行一次。 It doesn't run over and over and over. 它不会一遍又一遍地运行。 You click the button once, it is called once. 您单击一次按钮,它将被调用一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM