Changing the text on a label

前端 未结 6 1509
长情又很酷
长情又很酷 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:43

    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.

    0 讨论(0)
  • 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('<Return>', 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!

    0 讨论(0)
  • 2020-11-28 10:49

    Use the config method to change the value of the label:

    top = Tk()
    
    l = Label(top)
    l.pack()
    
    l.config(text = "Hello World", width = "50")
    
    0 讨论(0)
  • 2020-11-28 10:50

    I made a small tkinter application which is sets the label after button clicked

    #!/usr/bin/env python
    from Tkinter import *
    from tkFileDialog import askopenfilename
    from tkFileDialog import askdirectory
    
    
    class Application:
        def __init__(self, master):
            frame = Frame(master,width=200,height=200)
            frame.pack()
    
            self.log_file_btn = Button(frame, text="Select Log File", command=self.selectLogFile,width=25).grid(row=0)
            self.image_folder_btn = Button(frame, text="Select Image Folder", command=self.selectImageFile,width=25).grid(row=1)
            self.quite_button = Button(frame, text="QUIT", fg="red", command=frame.quit,width=25).grid(row=5)
    
            self.logFilePath =StringVar()
            self.imageFilePath = StringVar()
            self.labelFolder = Label(frame,textvariable=self.logFilePath).grid(row=0,column=1)
            self.labelImageFile = Label(frame,textvariable = self.imageFilePath).grid(row = 1,column=1)
    
            def selectLogFile(self):
                filename = askopenfilename()
                self.logFilePath.set(filename)
    
            def selectImageFile(self):
                imageFolder = askdirectory()
                self.imageFilePath.set(imageFolder)
    
    root = Tk()
    root.title("Geo Tagging")
    root.geometry("600x100")
    app = Application(root)
    root.mainloop()
    
    0 讨论(0)
  • 2020-11-28 10:55

    Here is another one, I think. Just for reference. Let's set a variable to be an instantance of class StringVar

    If you program Tk using the Tcl language, you can ask the system to let you know when a variable is changed. The Tk toolkit can use this feature, called tracing, to update certain widgets when an associated variable is modified.

    There’s no way to track changes to Python variables, but Tkinter allows you to create variable wrappers that can be used wherever Tk can use a traced Tcl variable.

    text = StringVar()
    self.depositLabel = Label(self.__mainWindow, text = self.labelText, textvariable = text)
                                                                        ^^^^^^^^^^^^^^^^^
      def depositCallBack(self,event):
          text.set('change the value')
    
    0 讨论(0)
  • 2020-11-28 11:02
    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')
    
    0 讨论(0)
提交回复
热议问题