Python Forum
Trying to update label text using a grid button.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to update label text using a grid button.
#1
I'm wanting to learn a couple simple text manipulations, like changing label and button text based on a button input or code.
But I can't seem to get a label's text to change based on a button input. See code.

import tkinter as tk

# Function to update label configuration
def update_label():
    label = tk.Label(root, text = "New text")
   
# Create the Tkinter window
root = tk.Tk()
root.title("Label text update demo")
root.geometry( "300x300" ) #(HxV)

# Create a label widget and place it in the grid
label = tk.Label(root, text="Original Label", bg="lightblue", padx=20, pady=10)
label.grid(row=0, column=0)

# Create a button to update label configuration
update_button = tk.Button(root, text="Update Label", command=update_label)
update_button.grid(row=1, column=0)
update_button1 = tk.Button(root, text = "Button 1")
update_button1.grid(row = 1, column = 1)

# Run the Tkinter event loop
root.mainloop()
(the Insert Code Snippet icon doesn't display [c...] & [/c...])

Any suggestions appreciated.
Reply
#2
Maybe this will help

import tkinter as tk

# Define update function - Default text for arg
def update_label(arg='New Text'):
    label.configure(text=arg)

root = tk.Tk()
label = tk.Label(root, text='Start Text', bg='lightyellow')
label.grid(column=0, columnspan=2, row=0, padx=5, pady=5, sticky='ew')

# This button will show default new text
button = tk.Button(root, text='Update Label', command=update_label)
button.grid(column=0, row=1, padx=5, pady=5)

# This button will show text passed as arg
button2 = tk.Button(root, text='Button 1', command=lambda:update_label('More Text'))
button2.grid(column=1, row=1, padx=5, pady=5)

root.mainloop()
Edward_ likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
Change the label text. Don’t create a new label
Reply
#4
Thanks so much.
I had searched and found examples using configure. But when I tested it with a direct quoted text value it didn't work.
Reply
#5
All of these will work

import tkinter as tk

# Define update function - Default text for arg
def update_label(arg='New Text'):
    label.configure(text=arg)
    label2.config(text='Direct quoted text will work too')
    label3['text'] = 'This works too.'

root = tk.Tk()
label = tk.Label(root, text='Start Text', bg='lightyellow')
label.grid(column=0, columnspan=3, row=0, padx=5, pady=5, sticky='ew')

label2 = tk.Label(root, text='Start Text', bg='lightyellow')
label2.grid(column=0, columnspan=3, row=1, padx=5, pady=5, sticky='ew')

label3 = tk.Label(root, text='Start Text', bg='lightyellow')
label3.grid(column=0, columnspan=3, row=2, padx=5, pady=5, sticky='ew')

# This button will show default new text
button = tk.Button(root, text='Update Label', command=update_label)
button.grid(column=0, row=3, padx=5, pady=5)

# This button will show text passed as arg
button2 = tk.Button(root, text='Button 1', command=lambda:update_label('More Text'))
button2.grid(column=1, row=3, padx=5, pady=5)

root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#6
So a follow-up question,

I'm trying this:
import tkinter as tk

def update(arg='Button pressed'):
    label.config(text=arg)
    
root = tk.Tk()
root.geometry( "300x300" )

label = tk.Label(root, text="4 Button Control")
label.pack(padx=20, pady=20)
 
frame = tk.Frame(root)
frame.pack(pady = 10, padx = 10)
 
button1 = tk.Button(frame, text = "Button 1")
button1.grid(row = 0, column = 0, padx = 5, pady = 5)
 
button2 = tk.Button(frame, text = "Button 2")
button2.grid(row = 0, column = 1, padx = 5, pady = 5)
 
button3 = tk.Button(frame, text = "Button 3")
button3.grid(row = 1, column = 0, padx = 5, pady = 5)
 
button4 = tk.Button(frame, text = "Button 4")
button4.grid(row = 1, column = 1, padx = 5, pady = 5, command=update("Test") # Causes TclError without a value, above example does not.
 
root.mainloop()
and on startup, its placing the "Test" word in the Label instead of the default text for the label.

Any thoughts as to why appreciated.
Reply
#7
"command" is not an argument for grid()
Quote:button4.grid(row = 1, column = 1, padx = 5, pady = 5, command=update("Test"))
"command" should be only be used in the __init__() or config()/configure() methods.

You could try this. Syntactically it is correct, but it produces the wrong results.
button4 = tk.Button(frame, text = "Button 4", command=update("Test")
The problem here is the code calls update("Test") and assigns the return value (None) to command. The label is immediately set to "Test" when the program starts running, and pressing "Button 4" does nothing because there is no command attached to the button press.

You probably want to do this.
button4 = tk.Button(frame, text = "Button 4", command=lambda: update("Test")
This creates a lambda expression that is called when the button is pressed. The lambda expression calls update() and passes the argument "Test". It is very similar to doing something like this:
def update_test()
    update("Test")

button4 = tk.Button(frame, text = "Button 4", command=update_test)
Edward_ likes this post
Reply
#8
Embarrased, I had the , command=lambda: update("Test") on the wrong line.
Tanks again Dean
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Update Image on Button Click the_muffin_man 2 1,226 Nov-05-2024, 01:29 AM
Last Post: menator01
  update text variable on label with keypress knoxvilles_joker 5 7,628 May-31-2024, 02:09 PM
Last Post: menator01
  Button to +1 in text box everytime it's clicked martyloo 1 1,383 May-01-2024, 02:32 PM
Last Post: Axel_Erfurt
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 1,827 Aug-20-2023, 04:45 PM
Last Post: menator01
  Centering and adding a push button to a grid window, TKinter Edward_ 15 14,288 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] how to make label or button not visible with the place method? nowayj63 2 5,267 Jan-03-2023, 06:29 PM
Last Post: Yoriz
  Can't change the colour of Tk button text Pilover 6 19,894 Nov-15-2022, 10:11 PM
Last Post: woooee
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 5,556 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 10,265 Jun-26-2022, 06:26 PM
Last Post: menator01
  [WxPython] [SOLVED] How to change button label? Winfried 3 3,444 May-31-2022, 06:37 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020