开发者

python3 tkinter, trying to update a label

开发者 https://www.devze.com 2023-03-13 11:07 出处:网络
I\'ve looked at some other answers here, but I\'m not understanding how to do this. This is the best I\'m coming up with.

I've looked at some other answers here, but I'm not understanding how to do this. This is the best I'm coming up with.

To eliminate off-subject comments, i prefer grid over pack, and I also like the widget.configure way of doing things, so that exactly one logical thing is accomplished with each line of code.

towards the end of the code, i have self.root.update() I have also left off the self. with no luck.

from tkinter import *

class Application:
    def __init__(self, maste开发者_开发知识库r):
        frame1 = Frame(master)
        frame1.grid(row=1, column=1)

        self.btnQuit = Button(frame1)
        self.btnQuit.configure(text="QUIT")
        self.btnQuit.configure(fg="red")
        self.btnQuit.configure(command=frame1.quit)
        self.btnQuit.grid(row=1, column=1)

        self.btnHi = Button(frame1)
        self.btnHi.configure(text="hi there")
        self.btnHi.configure(command="self.hello")
        self.btnHi.grid(row=2, column=1)

        self.lblMessage = Label(frame1)
        self.lblMessage.grid(row=2, column=2)

        def hello(self):
            self.lblMessage.configure(text="hello there")
            self.root.update()

root = Tk()
program = Application(root)
root.mainloop()


As stated above:

self.btnHi.configure(command="self.hello")

should be:

self.btnHi.configure(command=self.hello)

and mind the indent of the lines. But also:

self.v = StringVar()
self.lblMessage = Label(frame1, textvariable=self.v)

In your hello method, use:

self.v.set("hello there")


  1. You have to give the command option a callable object - not a string to be eval'd (which wouldn't work anyway since it'd be in a very different scope, e.g. with no/a different self). http://infohost.nmt.edu/tcc/help/pubs/tkinter/button.html seems to confirm this.
  2. The way your code is indented in your question, you define a local function hello inside __init__ instead of defining it at class level (as a method). You need to remove one level of indentation starting with def hello


Not clear to me what you are trying to do, but if you are trying to change the contents of a Label after it is created, then you need to use a StringVar() object. Setting that to the Label's textvariable parameter (in its constructor) will mean that the Label updates whenever you update the value of the StringVar (using its set() method).


Please try this:

from tkinter import *

    class Application:
        def __init__(self, master):
            frame1 = Frame(master)
            frame1.grid(row=1, column=1)

            self.btnQuit = Button(frame1)
            self.btnQuit.configure(text="QUIT")
            self.btnQuit.configure(fg="red")
            self.btnQuit.configure(command=frame1.quit)
            self.btnQuit.grid(row=1, column=1)

            self.btnHi = Button(frame1)
            self.btnHi.configure(text="hi there")
            self.btnHi.configure(command=self.hello)
            self.btnHi.grid(row=2, column=1)

            self.lblMessage = Label(frame1)
            self.lblMessage.grid(row=2, column=2)

        def hello(self):
            self.lblMessage.configure(text="hello there")
            self.root.update()

    root = Tk()`enter code here`
    program = Application(root)
    root.mainloop()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号