简体   繁体   中英

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.

I've got the following code which basically creates a dictionary called states in which the item is linked to a value of 0.

I then launch the 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. I though mainloop() repeated the code above it over and over again.
  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() . 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 never "refreshes values of dictionaries". 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. I though mainloop() repeated the code above it over and over again.

No, mainloop does not repeat code that is above it. The code above it runs exactly once. mainloop() is a simple loop that waits for events and responds to them based on bindings. No code will run after mainloop until the window is destroyed.

  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().

It's unclear which button you're talking about. However, it looks like both buttons that call is_true will actually change the value. 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.

Again, the code in Station runs exactly once when it is called. It doesn't run over and over and over. You click the button once, it is called once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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