Update Tkinter Label from Variable

Update Tkinter Label from variable

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.

How to make a Tkinter label update?

Configuring the text of a label is a one shot effect. Updating the int later won't update the label.

To solve this, you can either explicitly update the label yourself:

def add():
global x
x += 1
label.configure(text=x)

... Or you can use a tkinter variable like an IntVar (or more generally, a StringVar, if your text isn't just a number), which does update the label when you update the var. Don't forget to configure textvariable instead of text if you do this.

from tkinter import *

win = Tk()

x = IntVar()
x.set(1)
def add():
x.set(x.get() + 1)

label = Label(win, textvariable=x)
label.pack()

button = Button(win, text="Increment", command=add)
button.pack()

win.mainloop()

Take care to create a Tk() instance before you create the IntVar, otherwise tkinter will throw an exception.

Tkinter Label referring to variable doesn't update when variable changed

Welcome to Stackoverflow. First of all, you don't really need to create seperate classes for label and button in your program. You can create them inside of main class as tkinter widgets. Then if you want to pass a variable of an instance of some class, you need to initiliaze it and pass to your update_letter function properly, which you can use lambda.
Here is an example code that you can work on:

import tkinter as tk

class UpdateLabel:

def __init__(self, master):
self.master = master
# Create instance of Letter class
a = Letter(value=0)
current_letter = a
self.update_button = tk.Button(master, text='Update', command=lambda:self.update_letter(current_letter))
self.update_button.grid(row=0, column=0)

self.label = tk.Label(master, text='No Value')
self.label.grid(row=1, column=0)

def update_letter(self, current_letter):
print("current_letter.value before: " + str(current_letter.value))
current_letter.value += 1
print("current_letter.value now: " + str(current_letter.value))
self.label.configure(text='Value: {}'.format(current_letter.value))

class Letter:
def __init__(self, value):
self.value = value

if __name__ == '__main__':
root = tk.Tk()
app = UpdateLabel(master=root)
root.mainloop()

In that link How to change label text, you can find other options for changing a text in tkinter label widget.
I hope it helps

update label variable in python tkinter

Here it is. Now you can see that the label changes:

from tkinter import *
import tkinter as tk
import random

def test_print():
f = random.randint(0, 118)
label.configure(text=str(f))
master.update_idletasks()

# creating Tk window
master = Tk()

master.minsize(600, 400)

# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill=BOTH, expand=True)

# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text="Click me !",
background="red", fg="white", command=test_print)
b1.pack(side=TOP, expand=True, fill=BOTH)

label = tk.Label(pane)
label.pack(side=BOTTOM, expand=False)

# Execute Tkinter
master.mainloop()

I am trying to update a tkinter label with a variable, but the label just comes up blank

As @acw1668 noticed - textvariable= needs StringVar, not string - so you have to assing selected_park without .get() (because .get() gives you string).

 selected_park = tk.StringVar()

tk.Label(.... textvariable=selected_park, ...) # without `.get()`

And now you can change text in Label using

 selected_park.set("new_text")

Cannot get StringVar to update Label

You need to provide parameter textvariable=save_diretory to actually tell Label to reflect the changes made to the StringVar()

L4 = tkinter.Label(top, text=save_directory.get(),textvariable=save_directory)
L4.grid(row = 2, column = 2, sticky = W, pady = 2)

According to Geeks for Geeks

textvariable is associated with a Tkinter variable (usually a StringVar) with the label. If the variable is changed, the label text is updated.

Tkinter update label with variable from inside function

Tkinter Variables are different from normal variables. To create one:

label_text = tk.StringVar()

Then, rather than assigning to the variable, you nee to use the set method:

label_text.set('')

or

label_text.set('Not Valid')

See: http://effbot.org/tkinterbook/variable.htm



Related Topics



Leave a reply



Submit