小编edi*_*ate的帖子

更改标签上的文字

我在使用键绑定来更改标签或任何参数的值时遇到问题.这是我的代码:

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()
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我点击输入框并按Enter键,希望标签将值更改为"更改值".但是,虽然它确实打印了该文本,但标签保持不变.

通过查看有关类似问题和问题的其他问题,我已经想出了如何在课堂外使用其中的一些,但是我在课堂上做这件事有些困难.

另外,在旁注中,"主人"在tkinter中扮演什么角色?

python tkinter python-3.x

35
推荐指数
3
解决办法
15万
查看次数

标签 统计

python ×1

python-3.x ×1

tkinter ×1