set label text python tkinter code example
Example 1: tkinter change label text
pythonCopyimport tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.text = tk.StringVar()
self.text.set("Test")
self.label = tk.Label(self.root, textvariable=self.text)
self.button = tk.Button(self.root,
text="Click to change text below",
command=self.changeText)
self.button.pack()
self.label.pack()
self.root.mainloop()
def changeText(self):
self.text.set("Text updated")
app=Test()
Example 2: how to create a label in tkinter
from tkinter import *
new_window = Tk()
new_window.title("My Python Project")
new_window.geometry("300x250")
Label(new_window, text = "Hello World").pack()
Example 3: how to change the text of a label in tkinter
def press():
Instruction.config(text='Button Pressed')