python Tkinter label changing text value by Button Command
Asked Answered
V

2

1

I am using Tkinter Label widget to display some text to my UI Frame and I want the Label to change the text every-time I click the button. In my case I got wrong... it didn't change, is it possible?

This is my code..

currentCounterNumber = "0"

def counterPlus(teller_num):
    #.... the data is working well ....
    data = s.recv(1024) 
    if data:
        currentCounterNumber = data
......
class Content(tk.Frame):
def __init__(self, master, teller_name,*args, **kwargs):
    tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
    self.L4 = tk.Label(self, text="Serving # " + currentCounterNumber +"!")
    self.L4.pack( side = "top", fill="both", expand=False)      

    self.button1 = tk.Button(self, text="+", width=15, command=lambda: counterPlus(teller_no))
    self.button1.pack(side = "top", fill="both", expand=True)
Viper answered 19/10, 2013 at 4:40 Comment(1)
Move the counterPlus method to the Content class, add Content.L4 as an argument to counterPlus or make a changeL4Text(newText) method in Content, because you need to access it to change its text.Slate
U
6

Assuming that content_obj = Content(....) is defined.

You can change the text using :

content_obj.L4['text'] = "Serving # {}!".format(currentCounterNumber)

or

content_obj.L4.configure(text="Serving # {}!".format(currentCounterNumber))
#       OR     config

Example:

from Tkinter import * # Python 3.x: from tkinter import *

def advance():
    lb['text'] = str(int(lb['text']) + 1)
    root.after(1000, advance)

root = Tk()
lb = Label(root, text='0')
lb.pack()
root.after(1000, advance)
root.mainloop()
Unship answered 19/10, 2013 at 5:1 Comment(4)
thanks for the replu falsetru, do have an example of button command that changes the Label text?Viper
@gadss, Sorry, but I don't understand what you are asking.Unship
@gadss, Example code: If you click the button, the text of the label is changed.Unship
I try to add content_obj.L4['text'] = "Serving # {}!".format(currentCounterNumber) on my def counterPlus(teller_num) but it seems the self.L4 label didn`t changed the text valueViper
C
0

Set the text to a tkinter variable:

currentCounterNumber = 0
labelText = tk.StringVar()
labelText.set(f"Serving #{currentCounterNumber}!")

def counterPlus(teller_num):
    #.... the data is working well ....
    data = s.recv(1024) 
    if data:
        currentCounterNumber = data
        labelText.set(f"Serving #{currentCounterNumber}!")
......
class Content(tk.Frame):
    def __init__(self, master, teller_name,*args, **kwargs):
        tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
        self.L4 = tk.Label(self, textvariable=labelText)
        self.L4.pack( side = "top", fill="both", expand=False)      

        self.button1 = tk.Button(self, text="+", width=15, command=lambda: counterPlus(teller_no))
        self.button1.pack(side = "top", fill="both", expand=True)
Cenesthesia answered 7/7, 2023 at 9:33 Comment(2)
Yes, I'm a bit late but at least if anyone has the same problem they can find a solution.Cenesthesia
Add an explanation of your code to your answer.Indiana

© 2022 - 2024 — McMap. All rights reserved.