【发布时间】:2021-04-12 09:36:56
【问题描述】:
我的程序应该是一个非常简单的计数器,但我无法弄清楚为什么在单击按钮时它不计数。
from tkinter import *
class Counter:
def __init__(self):
self.__value = 0
self.__main_window = Tk()
self.__current_value = Label(self.__main_window, text=self.__value)
self.__current_value.pack()
self.__increase_button = Button(self.__main_window, text='Increase',
command=self.increase)
def increase(self):
self.__value += 1
def main():
Counter()
if __name__ == "__main__":
main()
【问题讨论】:
-
你的意思是不要缩进
def increase()那么远? -
嗨,对不起,它被放置了额外的 4 个空格,我现在已经修复了。但现在我的代码原来是这样的,这不起作用
-
我已经回答了问题的症结,但我也注意到您的
self.__increase_button按钮永远不会打包到 GUI 中并且不会显示。您可能希望在声明变量后添加self.__increase_button.pack()。 -
也非常感谢您在这些方面提供帮助,我是 GUI 新手,所以我一直忘记那些小细节。它对您的评论也很有帮助!
标签: python class tkinter counter