在主目录中打印更新的标签变量

时间:2018-10-12 11:10:02

标签: python tkinter label

我想读出label变量的当前值,以便稍后将其发送到8x7段显示器。现在,我打印它以检查值,我只返回0,而不是当前值。

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

root.mainloop()


if __name__ == '__main__':
    main()

我认为它将与.get()一起使用,但这似乎是错误的。

2 个答案:

答案 0 :(得分:0)

您正在执行counteraway.ca.get()函数之前执行main(),因此counteraway变量尚未初始化。因此,counteraway.ca.get()无法正常工作。

如果你放

if __name__ == '__main__':
     main()

之前

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

root.mainloop()

,它可能会起作用。

答案 1 :(得分:0)

发布python代码时,请务必确保缩进正确。

打印0的原因是因为它是执行print counteraway.ca.get()时变量的值。如果将值打印在一个通过按钮更新值的函数中,则该值将正确打印。

例如,如果您将_update_counter函数更改为此:

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

每次单击按钮更新值时,您都会看到它打印出正确的值。

固定缩进后,完整代码如下:

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))
        print self.ca.get()

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

    root.mainloop()


if __name__ == '__main__':
    main()