更改标签上的文本 [英] Changing the text on a label
问题描述
我在使用键绑定更改标签或任何参数的值时遇到问题.这是我的代码:
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.
另外,顺便提一下,master"在 tkinter 中扮演什么角色?
Also, on a side note, what role does "master" play in tkinter?
推荐答案
self.labelText = 'change the value'
上面这句话让labelText改变了值,但不改变depositLabel的文本.
The above sentence makes labelText change the value, but not change depositLabel's text.
要更改 depositLabel 的文本,请使用以下设置之一:
To change depositLabel's text, use one of following setences:
self.depositLabel['text'] = 'change the value'
或
self.depositLabel.config(text='change the value')
这篇关于更改标签上的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!