Tkinter wit while loop (python 3) -
Tkinter wit while loop (python 3) -
first created window buttons , defined commands. works fine until add together while loop check if button pressed , move next step. window doesn't show , loop running forever. know if there improve alternative code.
from tkinter import * round = 0 def blackc(): global round print ('0') x = 0 round += 1 def brownc(): global round print ('1') x = 1 round +=1 def redc(): global round print ('2') x = 2 round += 2 def win(): window = tk() window.geometry ('500x500') window.title('hello') blackb = button(text = 'black', command=blackc, width=7, height=3, bd=5) blackb.place(x=1, y=1) brownb = button(text = 'brown', command=brownc, width=7, height=3, bd=5) brownb.place(x=86, y=1) redb = button(text = 'red', command=redc, width=7, height=3, bd=5) redb.place(x=172, y=1) window.mainloop() while (round == 0): win() while (round < 3): if (round == 1): y = x * 10 print ('y') elif (round == 2): y += x print ('y')
i don't know mean moving next step, misunderstand how tkninter works. missing parenthesis in mainloop window.mainloop()
. , don't want phone call in cycle, because mainloop function cycle. run 1 time time runs infinitely. code have run 1 time time function win().
from tkinter import * round=0 def button(type): global round print (str(type)) x = type round += type def win(): window = tk() window.geometry ('500x500') window.title('hello') blackb = button(text = 'black', command=lambda: button(0), width=7, height=3, bd=5) blackb.place(x=1, y=1) brownb = button(text = 'brown', command=lambda: button(1), width=7, height=3, bd=5) brownb.place(x=86, y=1) redb = button(text = 'red', command=lambda: button(2), width=7, height=3, bd=5) redb.place(x=172, y=1) window.mainloop win()
you have asked improve code, have rewritten buttons func one, takes parametr of type , phone call lambda function (take look: http://www.diveintopython.net/power_of_introspection/lambda_functions.html. larger projects improve have tkinter window class, in enough.
python tkinter
Comments
Post a Comment