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
#1
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=StringVar(self.root)
      self.var2=StringVar(self.root)
      self.line1=CL.CustomFont_Label(self.root,textvariable=self.var1,
font_path=fontPath,size=36).grid(row=0,column=0,columnspan=5)
      self.line2=CL.CustomFont_Label(self.root,textvariable=self.var2,
font_path=fontPath,size=36).grid(row=1,column=0,columnspan=5)
      self.var1.set(self.pad('Stoker II'))
      self.var2.set(self.pad('Initializing'))
.
.
.
      self.root.mainloop()
Can someone give me the right syntax for changing label text? TIA.
Reply
#2
Not exactly sure what your looking for but, this works with textvariable
#! /usr/bin/env python3.8
'''Docstring'''
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)
        self.new_button()


    def update_text(self, textvar):
        self.lbl_search_text.set(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):
        self.lbl_search.destroy()
        MyClass(self.master)



root = tk.Tk()
root.geometry('300x200+50+50')
MyClass(root)
root.mainloop()
Output:
Apple Orange Peanuts
Reply
#3
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.
Reply
#4
Your code does not show the definition of CustomFont_Label
Reply
#5
I updated my previous post while you were adding yours. I just remembered it is not a normal Label. Doh!
Reply
#6
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?
Reply
#7
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.
Reply


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

Forum Jump:

User Panel Messages

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