Answers for "use button to change text of label tkinter"

1

python change label text with button

from tkinter import *

root = Tk()

# Option 1:
def changeText():
  label.set("Updated text")

label = StringVar()
label.set("Test text")

Label(root, textvariable=label).pack()
Button(root, text="Change text", command=changeText).pack()

# Option 2:
def change_text():
  my_label.config(text="New text")

global my_label
my_label = Label(root, text="First text")
my_label.pack(pady=5)

my_button = Button(root, text="Change text", command=change_text)
my_button.pack()

root.mainloop()
Posted by: Guest on May-04-2021

Code answers related to "use button to change text of label tkinter"

Python Answers by Framework

Browse Popular Code Answers by Language