I am having trouble with using a key binding to change the value of a label or any parameter. This is my code:
from tkinter import* class MyGUI: def __init__(self): self.__mainWindow = Tk() #self.fram1 = Frame(self.__mainWindow) self.labelText = 'Enter amount to deposit' self.depositLabel = Label(self.__mainWindow, text = self.labelText) self.depositEntry = Entry(self.__mainWindow, width = 10) self.depositEntry.bind('<Return>', self.depositCallBack) self.depositLabel.pack() self.depositEntry.pack() mainloop() def depositCallBack(self,event): self.labelText = 'change the value' print(self.labelText) myGUI = MyGUI()
When I run this, I click the entrybox and hit enter, hoping that the label will change value to 'change the value'. However, while it does print that text, the label remains unchanged.
From looking at other questions on similar problems and issues, I have figured how to work with some of this outside a class, but I'm having some difficulties with doing it inside a class.
Also, on a side note, what role does "master" play in tkinter?
Label text Property to Change/Update the Python Tkinter Label Text. Another solution to change the Tkinter label text is to change the text property of the label. The text of the label could be initiated with text="Text" and could also be updated by assigning the new value to the text key of the label object.
The change event is fired for <input> , <select> , and <textarea> elements when a change to the element's value is committed by the user... You won't be able to use the change event for an element that is not one of the previously mentioned types.
A good approach to label text is defining clear rules of what should receive which label. Once you do a list of rules, be consistent. If you classify profanity as negative, don't label the other half of the dataset as positive if they contain profanity.
self.labelText = 'change the value'
The above sentence makes labelText change the value, but not change depositLabel's text.
To change depositLabel's text, use one of following setences:
self.depositLabel['text'] = 'change the value'
OR
self.depositLabel.config(text='change the value')
You can also define a textvariable
when creating the Label, and change the textvariable to update the text in the label. Here's an example:
labelText = StringVar() depositLabel = Label(self, textvariable=labelText) depositLabel.grid() def updateDepositLabel(txt) # you may have to use *args in some cases labelText.set(txt)
There's no need to update the text in depositLabel
manually. Tk does that for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With