Changing the text on a label

前端 未结 6 1516
长情又很酷
长情又很酷 2020-11-28 10:43

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 __ini         


        
6条回答
  •  星月不相逢
    2020-11-28 10:44

    There are many ways to tackle a problem like this. There are many ways to do this. I'm going to give you the most simple solution to this question I know. When changing the text of a label or any kind of wiget really. I would do it like this.

    Name_Of_Label["text"] = "Your New Text"
    

    So when I apply this knowledge to your code. It would look something like this.

    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('', self.depositCallBack)
        self.depositLabel.pack()
        self.depositEntry.pack()
    
        mainloop()
    
      def depositCallBack(self,event):
        self.labelText["text"] = 'change the value'
        print(self.labelText)
    
    myGUI = MyGUI()
    

    If this helps please let me know!

提交回复
热议问题