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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


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
  Can't change the colour of Tk button text Pilover 6 14,506 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,766 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,672 Jun-26-2022, 06:26 PM
Last Post: menator01
  [WxPython] [SOLVED] How to change button label? Winfried 3 2,020 May-31-2022, 06:37 PM
Last Post: Winfried
  tkinter change the text of the checkbox zazas321 1 3,759 Sep-17-2021, 06:19 AM
Last Post: zazas321
  update text variable on label with keypress knoxvilles_joker 3 4,841 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  How to dynamically change radiobutton text kenwatts275 2 3,285 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  How to read text in kivy textinput or Label jadel440 1 5,184 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  [PyQt] Increase text size and change color based on temp pav1983 5 3,097 Jun-22-2020, 10:52 PM
Last Post: menator01
  [Kivy] Kivy text label won't shows up! AVD_01 1 2,895 Jun-21-2020, 04:01 PM
Last Post: AVD_01

Forum Jump:

User Panel Messages

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