在main的Label中写一个类变量

时间:2018-10-11 20:27:01

标签: python class tkinter global

我想在主目录中创建的标签中写入类的变量,但无法获取更新后的值。 也许有人可以帮我,因为我没办法了。

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 克拉罗

1 个答案:

答案 0 :(得分:0)

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

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()