在 main 的 Label 中写一个 class 变量

Write a class variable in a Label in main

我想将class的变量写入我在main中创建的标签中,但我无法获得更新后的值。 也许有人可以帮助我,因为我 运行 没有想法。

from Tkinter import *

class CounterAway(Frame):
    def __init__(self, parent=None, **kw):        
        Frame.__init__(self, parent, kw)
        self.countera = 0
        self.ca = 0
        self._update_counter()

def _update_counter(self):
    self.ca = self.countera       

def count_up(self):
    self.countera += 1
    if self.countera > 99 : self.countera = 0
    self._update_counter()

def count_down(self):
    self.countera -= 1
    if self.countera < 0 : self.countera = 0
    self._update_counter()

def main():
    root = Tk()
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()  
    """root.geometry("%dx%d+0+0" % (w, h))"""
    root.geometry('1000x1000')  
    counteraway = CounterAway(root)



Button(root, font=('Arial',30), width=10, text='Away +', command=counteraway.count_up).place(x=450, y=300)   
Button(root, font=('Arial',30), width=10, text='Away -', command=counteraway.count_down).place(x=450, y=370)
Button(root, width=10, font=('Arial',30), text='Quit', command=root.destroy).place(x=10, y=10)
counteraway.label = Label(root, font="Arial 100 bold", fg="RED", text=str(counteraway.ca)).pack()

print(counteraway.ca)

root.mainloop()

if __name__ == '__main__':
    main()

非常感谢您的帮助。

br 克拉罗

您在 main() 中定义了 root,然后尝试在此函数之外使用它,这意味着代码将抛出异常。似乎定义的 class 的缩进不正确,如果您将代码更改为以下内容,它应该 运行:

from Tkinter import *

class CounterAway(Frame):
    def __init__(self, parent=None, **kw):        
        Frame.__init__(self, parent, kw)
        self.countera = 0
        self.ca = 0
        self._update_counter()

    def _update_counter(self):
        self.ca = self.countera       

    def count_up(self):
        self.countera += 1
        if self.countera > 99 : self.countera = 0
        self._update_counter()

    def count_down(self):
        self.countera -= 1
        if self.countera < 0 : self.countera = 0
        self._update_counter()


def main():
    root = Tk()
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()  
    """root.geometry("%dx%d+0+0" % (w, h))"""
    root.geometry('1000x1000')  
    counteraway = CounterAway(root)

    Button(root, font=('Arial',30), width=10, text='Away +', command=counteraway.count_up).place(x=450, y=300)   
    Button(root, font=('Arial',30), width=10, text='Away -', command=counteraway.count_down).place(x=450, y=370)
    Button(root, width=10, font=('Arial',30), text='Quit', command=root.destroy).place(x=10, y=10)
    counteraway.label = Label(root, font="Arial 100 bold", fg="RED", text=str(counteraway.ca)).pack()

    print(counteraway.ca)

    root.mainloop()


if __name__ == '__main__':
    main()

如果您想更新标签中的文本,您应该查看 Tkinter 变量,例如 Tkinter StringVar。代码看起来像这样:

from Tkinter import *

class CounterAway(Frame):
    def __init__(self, parent=None, **kw):        
        Frame.__init__(self, parent, kw)
        self.countera = 0
        self.ca = StringVar()
        self._update_counter()

    def _update_counter(self):
        self.ca.set(str(self.countera))       

    def count_up(self):
        self.countera += 1
        if self.countera > 99 : self.countera = 0
        self._update_counter()

    def count_down(self):
        self.countera -= 1
        if self.countera < 0 : self.countera = 0
        self._update_counter()

def main():
    root = Tk()
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()  
    """root.geometry("%dx%d+0+0" % (w, h))"""
    root.geometry('1000x1000')  
    counteraway = CounterAway(root)

    Button(root, font=('Arial',30), width=10, text='Away +', command=counteraway.count_up).place(x=450, y=300)   
    Button(root, font=('Arial',30), width=10, text='Away -', command=counteraway.count_down).place(x=450, y=370)
    Button(root, width=10, font=('Arial',30), text='Quit', command=root.destroy).place(x=10, y=10)
    counteraway_label = Label(root, font="Arial 100 bold", fg="RED", textvariable=counteraway.ca).pack()

    print(counteraway.ca)

    root.mainloop()


if __name__ == '__main__':
    main()