![]() |
[Tkinter] Python 3 change label text - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Python 3 change label text (/thread-26625.html) |
Python 3 change label text - gw1500se - May-07-2020 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. RE: Python 3 change label text - menator01 - May-07-2020 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()
RE: Python 3 change label text - gw1500se - May-08-2020 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. RE: Python 3 change label text - Yoriz - May-08-2020 Your code does not show the definition of CustomFont_Label
RE: Python 3 change label text - gw1500se - May-08-2020 I updated my previous post while you were adding yours. I just remembered it is not a normal Label. Doh! RE: Python 3 change label text - gw1500se - May-08-2020 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? RE: Python 3 change label text - deanhystad - May-08-2020 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. |