gpt4 book ai didi

python - 在main中的Label中写入类变量

转载 作者:太空宇宙 更新时间:2023-11-03 21:42:14 25 4
gpt4 key购买 nike

我想在我在 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,然后尝试在该函数之外使用它,这意味着代码将引发异常。定义的类的缩进似乎也不正确,如果将代码更改为以下内容,它应该运行:

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

关于python - 在main中的Label中写入类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52768425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com