Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Tkinter Label from variable

I wrote a Python script that does some task to generate, and then keep changing some text stored as a string variable. This works, and I can print the string each time it gets changed.

I can get the Label to display the string for the first time, but it never updates.

Here's my code:

from tkinter import *  outputText = 'Ready' counter = int(0)  root = Tk() root.maxsize(400, 400)  var = StringVar()  l = Label(root, textvariable=var, anchor=NW, justify=LEFT, wraplength=398) l.pack()  var.set(outputText)  while True:     counter = counter + 1     outputText = result     outputText = result     outputText = result     if counter == 5:         break  root.mainloop() 

The Label will show Ready, but won't update to change that to the strings as they're generated later.

After a fair bit of googling and looking through answers on this site, I thought the solution might be to use update_idletasks. I tried putting that in after each time the variable was changed, but it didn't help.

like image 535
Tom Avatar asked Apr 08 '10 20:04

Tom


People also ask

Can you change label in tkinter?

This is the easiest one , Just define a Function and then a Tkinter Label & Button . Pressing the Button changes the text in the label.

What does update () do in tkinter?

Python Tkinter Mainloop Update Update() method in mainloop in Python Tkinter is used to show the updated screen. It reflects the changes when an event occurs.

What is label configure in tkinter?

The Label widget in tkinter is generally used to display text as well as image. Text can be added in a Label widget by using the constructor Label(root, text= "this is my text"). Once the Label widget is defined, you can pack the Label widget using any geometry manager.

What is Textvariable in tkinter?

In the case of textvariable , which is mostly used with Entry and Label widgets, it is a variable that will be displayed as text. When the variable changes, the text of the widget changes as well.


2 Answers

The window is only displayed once the mainloop is entered. So you won't see any changes you make in your while True block preceding the line root.mainloop().


GUI interfaces work by reacting to events while in the mainloop. Here's an example where the StringVar is also connected to an Entry widget. When you change the text in the Entry widget it automatically changes in the Label.

from tkinter import *  root = Tk() var = StringVar() var.set('hello')  l = Label(root, textvariable = var) l.pack()  t = Entry(root, textvariable = var) t.pack()  root.mainloop() # the window is now displayed 

I like the following reference: tkinter 8.5 reference: a GUI for Python


Here is a working example of what you were trying to do:

from tkinter import * from time import sleep  root = Tk() var = StringVar() var.set('hello')  l = Label(root, textvariable = var) l.pack()  for i in range(6):     sleep(1) # Need this to slow the changes down     var.set('goodbye' if i%2 else 'hello')     root.update_idletasks() 

root.update Enter event loop until all pending events have been processed by Tcl.

like image 101
Gary Kerr Avatar answered Sep 19 '22 13:09

Gary Kerr


Maybe I'm not understanding the question but here is my simple solution that works -

# I want to Display total heads bent this machine so I define a label - TotalHeadsLabel3 = Label(leftFrame) TotalHeadsLabel3.config(font=Helv12,fg='blue',text="Total heads " + str(TotalHeads)) TotalHeadsLabel3.pack(side=TOP)  # I update the int variable adding the quantity bent - TotalHeads = TotalHeads + headQtyBent # update ready to write to file & display TotalHeadsLabel3.config(text="Total Heads "+str(TotalHeads)) # update label with new qty 

I agree that labels are not automatically updated but can easily be updated with the

<label name>.config(text="<new text>" + str(<variable name>)) 

That just needs to be included in your code after the variable is updated.

like image 36
Brian Steinke Avatar answered Sep 18 '22 13:09

Brian Steinke