Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update label text after pressing a button in Tkinter

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?

like image 285
Yeo Jin Avatar asked Jan 09 '16 05:01

Yeo Jin


3 Answers

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"]

like image 194
furas Avatar answered Nov 13 '22 07:11

furas


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!

like image 31
Kishor K Avatar answered Nov 13 '22 07:11

Kishor K


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()
like image 45
los_g Avatar answered Nov 13 '22 08:11

los_g