Posts: 24
Threads: 8
Joined: May 2023
Dec-12-2024, 06:43 PM
(This post was last modified: Dec-12-2024, 06:43 PM by Edward_.)
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.
Posts: 1,145
Threads: 114
Joined: Sep 2019
Dec-12-2024, 07:22 PM
(This post was last modified: Dec-12-2024, 07:22 PM by menator01.)
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()
Posts: 6,788
Threads: 20
Joined: Feb 2020
Change the label text. Don’t create a new label
Posts: 24
Threads: 8
Joined: May 2023
Dec-13-2024, 03:45 PM
(This post was last modified: Dec-13-2024, 03:45 PM by Edward_.)
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.
Posts: 1,145
Threads: 114
Joined: Sep 2019
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()
Posts: 24
Threads: 8
Joined: May 2023
Dec-15-2024, 07:21 PM
(This post was last modified: Dec-15-2024, 07:21 PM by Edward_.)
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.
Posts: 6,788
Threads: 20
Joined: Feb 2020
Dec-16-2024, 02:57 AM
(This post was last modified: Dec-16-2024, 11:49 AM by deanhystad.)
"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)
Posts: 24
Threads: 8
Joined: May 2023
Dec-18-2024, 03:05 AM
(This post was last modified: Dec-18-2024, 03:05 AM by Edward_.)
Embarrased, I had the , command=lambda: update("Test") on the wrong line.
Tanks again Dean
|