Answers for "how to change the text of a label using a button i tkinet"

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
1

Update label text after pressing a button in Tkinter

#tested and working on PYTHON 3.8 AND ADDED TO PATH
import tkinter as tk
win = tk.Tk()

def changetext():
	a.config(text="changed text!")
    
a = tk.Label(win, text="hello world")
a.pack()
tk.Button(win, text="Change Label Text", command=changetext).pack()

win.mainloop()
Posted by: Guest on February-28-2020

Code answers related to "how to change the text of a label using a button i tkinet"

Python Answers by Framework

Browse Popular Code Answers by Language