Update label text after pressing a button in Tkinter
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"]
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!