python change label text with button

Solutions on MaxInterview for python change label text with button by the best coders in the world

showing results for - "python change label text with button"
Tiger
07 Jan 2020
1from tkinter import *
2
3root = Tk()
4
5# Option 1:
6def changeText():
7  label.set("Updated text")
8
9label = StringVar()
10label.set("Test text")
11
12Label(root, textvariable=label).pack()
13Button(root, text="Change text", command=changeText).pack()
14
15# Option 2:
16def change_text():
17  my_label.config(text="New text")
18
19global my_label
20my_label = Label(root, text="First text")
21my_label.pack(pady=5)
22
23my_button = Button(root, text="Change text", command=change_text)
24my_button.pack()
25
26root.mainloop()