Answers for "tkinter change label text with button"

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
3

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()
Posted by: Guest on December-26-2020

Code answers related to "tkinter change label text with button"

Python Answers by Framework

Browse Popular Code Answers by Language