Update label text after pressing a button in Tkinter
Asked Answered
P

6

9

I was wondering how to change the label text after clicking a button. For example:

from Tkinter import *
import tkMessageBox

def onclick():
    pass

root = Tk()

root.title("Pantai Hospital")

L1 = Label(root, text='Welcome to Pantai Hospital!')
L1.pack() 
L2 = Label(root, text='Login')
L2.pack() 

L3 = Label(root, text = "Username:")
L3.pack( side = LEFT, padx = 5, pady = 10)
username = StringVar()
E1 = Entry(root, textvariable = username, width = 40)
E1.pack ( side = LEFT)

L4 = Label(root, text = "Password:")
L4.pack( side = LEFT, padx = 5, pady = 10)
password = StringVar() 
E2 = Entry(root, textvariable = password, show = "*", width = 40)    
E2.pack( side = LEFT)'`

I want to change those labels username and password and the entry field into another different label after clicking a button. How do I do that?

Photoluminescence answered 9/1, 2016 at 5:5 Comment(2)
Split the task in two: 1. how to do anything (e.g., print("something")) on pressing a button? 2. how to change label text?Pruritus
first create button ;)Cloudless
C
13

Answer for "how to do anything on pressing button" should be in any tutorial.
For example in effbot book: Button

Use command= to assign function name to button.

(btw: function name (or callback) means name without parenthesis and arguments)

btn = Button(root, text="OK", command=onclick)

Answer for "how to change label text" should be in any tutorial too.

lbl = Label(root, text="Old text")

# change text

lbl.config(text="New text")

# or

lbl["text"] = "New text"

If you want to change Entry into Label then remove/hide Entry (widget.pack_forget()) or destroy it (widget.destroy()) and create Label.

btw: you can disable Entry instead of making Label (ent.config(state='disabled'))


EDIT: I removed dot in lbl.["text"]

Cloudless answered 9/1, 2016 at 11:28 Comment(2)
Thanks! it's very helpful.Photoluminescence
lbl.["text"] needs to be lbl["text"] Thanks.Carincarina
T
3

write lbl.pack() after you write the button.pack() A small snippet of code to display change in value on clicking a button. This is done so that the changes made in the label will be shown after you perform the button click.

    from tkinter import *

    root = Tk(className = "button_click_label")
    root.geometry("200x200")

    message = StringVar()
    message.set('hi')

    l1 = Label(root, text="hi")


    def press():
        l1.config(text="hello")

    b1 = Button(root, text = "clickhere", command = press).pack()

    l1.pack()

    root.mainloop()

Im just an entry level python programmer. Forgive , and do correct me if I'm wrong! Cheers!

Tepper answered 20/10, 2017 at 7:14 Comment(0)
D
2

Another way to alter a label dynamically. here we use lambda to show multiple adjustments to a label display. if you want to go with one label change simply disregard lambda and call the function without a parameter (in this case the 1 and 2). remember to ensure you separate .pack method when creating a label for this use or you'll get an error when the function attempts to config a line with the .pack method.

from tkinter import *  

root = Tk(className = "button_click_label")  
root.geometry("200x200")
  
def press(x):  
    if x == 1:  
        l1.config(text='hello')    
    else:    
        l1.config(text='hi')  

b1 = Button(root, text = "click me", command = lambda:press(1)).pack()  
b2 = Button(root, text = 'click me', command = lambda:press(2)).pack()

l1 = Label(root, text="waiting for click")  
l1.pack()

root.mainloop()
Dowie answered 4/1, 2021 at 10:53 Comment(0)
P
1

Here is an example where I created a basic gui with a label. Then I changed the label text.

import tkinter as tk
from tkinter import *
app = tk.Tk()
#creating a Label
label = Label(app,  text="unchanged")
label.pack()
#updating text 
label.config(text="changed")
app.mainloop()
Philina answered 9/11, 2017 at 16:23 Comment(0)
J
0

This should work:

from tkinter import *

root = Tk(className = "button_click_label")
root.geometry("200x200")

message = StringVar()
message.set('hi')

l1 = Label(root, text="hi")
l1.pack()

def press():
    l1.config(text="hello")

b1 = Button(root, text = "clickhere", command = press).pack()

root.mainloop()
Jazzy answered 20/3, 2020 at 21:52 Comment(0)
I
0

Here is a simple dice roller. In the on_click function you will see that label5 text is being changed to whatever dice was rolled, you can click the button many many times and get the label to change each and every time. I know this question has been answered multiple times but no one has suggested label["text"] = "some text". So i am adding it as an answer because it works:

from tkinter import *
import random

root = Tk()
root.title("Die Roller")

entry1 = Entry(root, width = 5)
entry2 = Entry(root, width = 5)

def on_click():
    r = 0
    for z in range(0, int(entry1.get())):
        r += random.randint(1, int(entry2.get()))
    label5["text"] = r

label1 = Label(root, text = "Die Roller!")
label2 = Label(root, text = "Number of Die")
label3 = Label(root, text = "Number of Sides")
label4 = Label(root, text = "Total")
label5 = Label(root, text = " ")
button = Button(root, text = "Roll", command = on_click)

label1.grid(column = 1, row = 0)
label2.grid(column = 0, row = 1)
label3.grid(column = 0, row = 2)
entry1.grid(column = 2, row = 1)
entry2.grid(column = 2, row = 2)
label4.grid(column = 0, row = 4)
label5.grid(column = 2, row = 4)
button.grid(column = 1, row = 3)

root.mainloop()
Isomer answered 8/1 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.