简体   繁体   中英

How to update a Label inside while loop in tkinter?

I am trying to update label of a window in tkinter inside a while loop but the text is not updating.Below is the code.

from Tkinter import *

window=Tk();
text1=StringVar();
text1.set("Waiting for Button Press...");

def main():
    while True:
          if condition:'''The condition is GPIO READ statement but for simplicity I have used condition'''
             text1.set("IN Button Pressed.Loading Camera.");
             window.update()

lbl=Label(window,text=text1.get());
lbl.pack();
window.after(5000,main);
window.mainloop();

To update the text of a label dynamically you can use lbl["text"]

I can not follow your code completely(because I'm too new to this...) but I think you can use this and rework the code that way.

from Tkinter import *
window=Tk()
text1 = "Waiting for Button Press..."
lbl=Label(window,text=text1)
lbl.pack()

def main():
    while True:
          if condition:'''The condition is GPIO READ statement but for simplicity I have used condition'''
             lbl["text"] = "IN Button Pressed.Loading Camera."

window.after(5000,main)
window.mainloop()

Not sure if you need the window.update()

As I said in a comment, using a while loop like you're attempting to do will interfere with Tkinter's own mainloop() . To do something repeatedly without stopping the Tkinter GUI's main loop is often accomplished by using the universal after() method (which you seem to know about already).

In addition, if you set the textvariable= (instead if text= ) option of the Label widget to the StringVar , changes to the Tkinter Variable will automatically update the Label 's appearance. Here's a little documentation I found about using Tkinter Variable Classes .

Here's example code showing how to accomplish your goal by implementing these suggestions:

from random import randint
from Tkinter import *

MS_DELAY = 500  # Milliseconds between updates.

window = Tk()
text1 = StringVar()

def gpio_read():
    """ Simulate GPIO READ. """
    return randint(0, 3)

def check_condition():
    if gpio_read() == 0:
        text1.set("IN Button Pressed.Loading Camera.")
    else:
        text1.set("Waiting for Button Press...")

    window.after(MS_DELAY, check_condition)  # Schedule next check.

lbl = Label(window, textvariable=text1)  # Link to StringVar's value.
lbl.pack()

window.after(MS_DELAY, check_condition)  # Schedule first check.
window.mainloop()

You could do something like this:

The condition is set to False , and is changed to True in main .

After 5 seconds, the change_if_condition method is called, condition is checked, and if found True , the label textvariable is changed, and automatically updates the label.

from tkinter import *


def change_if_condition():
    if condition:
        text1.set("IN Button Pressed.Loading Camera.")


def main():
    global condition
    condition= True
    window.after(5000, change_if_condition)


window = Tk()

text1 = StringVar()
text1.set("Waiting for Button Press...")

lbl = Label(window, textvariable=text1)   # use textvariable, so the displayed text is changed when the variable changes
lbl.pack()

condition = False

main()

window.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