-1

I'm trying to make a GUI using tkinter which has a refreshing display. I have this code,

while True:
    val= randint(1,50)

And this is the loop for my GUI,

label2= Label(root, text= val , font=("Helvetica", 60))
label2.place(relx=0.53, rely=0.44, anchor=CENTER)

root.mainloop()

These two do not seem to work simultaneously. The one placed above the other one executes first which is an infinite loop so it never reaches "root.mainloop()". I want to update the label content constantly. How do I do it?

Misha Melnyk
  • 409
  • 2
  • 5
  • Does this answer your question? [making-python-tkinter-label-widget-update](https://stackoverflow.com/questions/1918005) – stovfl May 06 '20 at 07:50
  • Either put the while loop in a thread task, or use `after()` to replace the while loop. – acw1668 May 06 '20 at 10:03

2 Answers2

0

One way to do this would be to use Tk.After(delay,callback). You can wrap the while loop in a function, and use it as the callback.

For example:

def f():
    val.set(randint(1,50))
    root.after(1,f)

#Later
root.after(1, f)
root.mainloop()

One thing that will also need to change is how you're setting val in Label. It should be textvariable=val instead of text=val

Misha Melnyk
  • 409
  • 2
  • 5
  • Hey thanks for the reply, I tried placing the **While** loop inside a function and then calling it. `def f(): while True: val= randint(1,50) time.sleep(3) root.after(2000, f) root.mainloop()` But it doesn't seem to work. The program never reaches **root.mainloop** due to the infinite **While loop** due to which the GUI Window never displays anything. – Harsh Pradhan May 06 '20 at 06:57
  • Is the window popping up an doesn't have anything on it, or does the window not even pop up? – Misha Melnyk May 06 '20 at 07:00
  • The window is popping up but doesn't have anything on it. – Harsh Pradhan May 06 '20 at 07:01
  • This is a matter of updating the variable inside of the while loop. I can take a look on how to do that, don't remember off the top of my head. – Misha Melnyk May 06 '20 at 07:03
  • Yes, please... That'd be of great help. I have also tried the **val= StringVar()** and **val.set('something')** for updating the label content but the problem sticks around the Infinite While I Guess. – Harsh Pradhan May 06 '20 at 07:06
  • I was wrong before, the window not popping up was probably something to due with Tkinter scheduling the updates of the window, with it waiting for functions called with `root.after()` to finish. I made an edit where the function is called recursively and updates the variable. – Misha Melnyk May 06 '20 at 07:12
  • `root.after(1, f)` attempts to run `f` 1000 times per second. While you can attempt to do that, tkinter won't be able to keep up. – Bryan Oakley May 06 '20 at 14:43
  • Not sure exactly what you mean, tkinter will schedule screen updates after each one, so updating shouldn't be a problem – Misha Melnyk May 07 '20 at 16:05
0

Finally I was able to fix it using this,

while True:
    val= randint(1,50)
    label2.config(text=val)
    label2.after(200, self.get_upvotes)

label2= Label(root, font=("Helvetica", 60))
label2.place(relx=0.53, rely=0.33, anchor=CENTER)