简体   繁体   中英

Python 2.7 Tkinter redraw widget inside if

I have problem with my Tkinter code in Python 2.7. I have main window with one widget (button). I want to redraw window (and change value of variable -> add one widget) after click on the button. Where is problem? I think that problem can be, that every loop of mainloop change variable to 0. Thank you!

from Tkinter import *

def function():
    global variable
    variable = 0
    main.update()

variable = 0
main = Tk() #New Tk window

if variable == 1:
    Checkbutton(main, text="test").pack()

Button(main, text="Change", command=function).pack()

main.mainloop()

You never set variable to 1, and you should use functions (and classes) when working with GUIs.

from Tkinter import *
main = Tk() #New Tk window
variable = 0

def function():
    global variable
    variable = 1
    newThing()

def newThing(): 
   global variable
   if variable==1:
       Checkbutton(main, text="test").pack()
   variable = 0

Button(main, text="Change", command=function).pack()
main.mainloop()

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