Hello I just wanted that the Label change/refresh during the loop, but it doesn’t work
This my code
fen1 = Tk()
v = StringVar()
Label(fen1,textvariable=v).pack()
i=0
while(1):
i=i+1
v.set(i)
fen1.mainloop()
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
here, try this:
That should give you a good example. However, it is important to note that during the while loop, you MUST call
root.update()otherwise your GUI will freeze until the loop completes (in this case it never does) and never show your numbers.Also note that you can call
update_label()from anywhere in your program. I just added it to the start button for example purposes.What was wrong with your code was that you had set the while loop free-floating and most importantly before your GUI’s mainloop. When you do this, since this loop is infinate, it never allows
Tkinterto start itsmainloop(). However, if you were to put the while loop after the mainloop, then that would never be executed until after you exit the GUI, this is because the mainloop is infinate until it is stopped (closing the GUI).So to fix this you simply put it in a function and call it later on during
Tkinter‘s mainloop. You can do this various ways as well, for example, you can use.after()to perform a specific task after a certain amount of time, or make it the command of a button to be run when pressed, ect., ect. .However, The proper code you should use is the following, as you do not really want infinate loops in your code (other then you mainloop).: