Python Forum
[Tkinter] Python 3 change label text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Python 3 change label text
I have a tkinter label that I want to change. I've found several articles on how to so that but none work. If I try to use 'textvariable' I get an error that 'text' is required. If I set 'text' to '' then it does not change when I set the variable.

      self.var1.set(self.pad('Stoker II'))
Can someone give me the right syntax for changing label text? TIA.
Not exactly sure what your looking for but, this works with textvariable
#! /usr/bin/env python3.8
import tkinter as tk
import random as rd
from functools import partial

class MyClass:
    def __init__(self, master):
        self.master = master
        self.master.columnconfigure(0, weight=1)
        self.master.rowconfigure(0, weight=1)
        self.lbl_search_text = tk.StringVar(value=self.randomtext(self.update_text))
        self.lbl_search = tk.Label(self.master, textvariable=self.lbl_search_text, bg='#fffeee')
        self.lbl_search.grid(row=0, column=0, sticky='new', pady=40)

    def update_text(self, textvar):

    def randomtext(self, myvar):
        mytext = ['Apple', 'Orange', 'Peaches', 'Tangerines', 'Banannas', 'Peanuts']
        rdword = rd.randint(0, len(mytext)-1)
        return mytext[rdword]

    def new_button(self):
        button = tk.Button(self.master, text='Change Word', command=partial(self.doit))
        button.grid(column=0, row=1, sticky='nw', pady=10, padx=10)

    def doit(self):

root = tk.Tk()
Apple Orange Peanuts
Oh, wait. I forgot I'm not making a true text label. To use my custom font I had to use this recipe. It creates an image so I need to figure out how to modify it so I can change the image.
Your code does not show the definition of CustomFont_Label
I updated my previous post while you were adding yours. I just remembered it is not a normal Label. Doh!
I think I figured out what to do. If I 'destroy' the custom label then I can replace it. The problem is, I can't figure out how to add 'destroy()' to that recipe. Can someone help? TIA.

BTW, I can simply overwrite the widget but I think that means I will accumulate lots of widgets that are no longer used. If I use the same variable for the replacement will garbage collection remove the old one?
You don't have to destroy the label. You can use configure to change the label image. Make sure you keep a handle for the image so garbage collection doesn't collect the image.

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to dynamically change radiobutton text kenwatts275 2 122 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  How to read text in kivy textinput or Label jadel440 1 242 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  [PyQt] Increase text size and change color based on temp pav1983 5 584 Jun-22-2020, 10:52 PM
Last Post: menator01
  [Kivy] Kivy text label won't shows up! AVD_01 1 820 Jun-21-2020, 04:01 PM
Last Post: AVD_01
  [Tkinter] Change Label Every 5 Seconds gw1500se 4 1,088 May-26-2020, 05:32 PM
Last Post: gw1500se
  [Tkinter] Change label for multiple frames Dandy_Don 3 647 Apr-30-2020, 02:22 PM
Last Post: Dandy_Don
  [Tkinter] how to update label text from list Roshan 8 1,057 Apr-25-2020, 08:04 AM
Last Post: Roshan
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 3,331 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [Tkinter] Label, align imported text from pandas kundrius 2 1,300 Dec-11-2019, 08:26 AM
Last Post: kundrius
  Make Label Text background (default color) transparent using tkinter in python barry76 1 10,665 Nov-28-2019, 10:19 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020