Tkinter - Python 3 - 如何在类窗口中添加一个增量计数器?

0 投票
1 回答
2778 浏览
提问于 2025-04-30 23:12
class AppetiserClass():
    root = Tk()
    root.title("Appetiser Page")
    root.geometry("1920x1080")


    meal1 = 0

    def plus1():
        global meal1
        meal1 = meal1 + 1
        DisplayButton["text"]=str(meal1)
        return

    def neg1():
        global meal1
        meal1 = meal1 + 1
        DisplayButton["text"]=str(meal1)
        return

    app = Frame(root)
    app.grid()

    Label(app, text = "", width = 75, height = 20).grid(row = 1, column = 0, sticky = N)

    DisplayButton = Button(app, text = meal1)
    DisplayButton.grid(column = 1, row = 2, sticky = W)
    DisplayButton.config(height = 10, width = 10 )

    Plus1Button = Button(app, text = "+1", command=plus1, bg="green")
    Plus1Button.grid(column = 2, row = 2, sticky = W)
    Plus1Button.config(height = 10, width = 10 )

    Neg1Button = Button(app, text = "-1", command=neg1, bg="green")
    Neg1Button.grid(column = 3, row = 2, sticky = W)
    Neg1Button.config(height = 10, width = 10 )

    root.mainloop()

我遇到的问题是,我给一个全局变量(meal1,值为0)设置了一个值,但当我按下 +1 或 -1 按钮时,"DisplayButton" 上没有显示任何值,并且我收到了这个错误信息: "NameError: global name 'DisplayButton' is not defined"。

"DisplayButton" 是我放置的一个按钮,用来显示一个值。就这么简单,但我却收到了这个错误信息。

如果我去掉类,只运行这段代码,在单个窗口中,代码就能正常工作。

如果有人能帮忙,我将非常感激!

暂无标签

1 个回答

1

如果你的缩进是正确的,问题并不是因为DisplayButton和meal1是全局变量,而是因为它们是类级别的,你没有以正确的方式访问它们。这意味着你应该使用self这个关键词来访问它们。(self并不是必须的——在类中的任何函数的第一个参数总是定义了一个变量,通过这个变量你可以访问同一个类中的其他成员——但在Python中,使用"self"是个约定。)你需要在这个类里的所有函数中添加self作为参数,像这样:

def neg1(self):

然后通过self来访问meal1和DisplayButton:

 self.meal1 += 1

还有:

 self.DisplayButton["text"] = str(meal1)

我重新写了你的类,这样类里面的所有重要内容都可以通过self被其他部分访问:

from tkinter import *

class AppetiserClass:
    meal1 = 0
    root = Tk()
    app = Frame(self.root)

    def __init__(self):
        self.root.title("Appetiser Page")
        self.root.geometry("1920x1080")

        self.app.grid()

        Label(self.app, text = "", width = 75, height = 20).grid(row = 1, column = 0, sticky = N)

        self.DisplayButton = Button(self.app, text = self.meal1)
        self.DisplayButton.grid(column = 1, row = 2, sticky = W)
        self.DisplayButton.config(height = 10, width = 10 )

        self.Plus1Button = Button(self.app, text = "+1", command=self.plus1, bg="green")
        self.Plus1Button.grid(column = 2, row = 2, sticky = W)
        self.Plus1Button.config(height = 10, width = 10 )

        self.Neg1Button = Button(self.app, text = "-1", command=self.neg1, bg="green")
        self.Neg1Button.grid(column = 3, row = 2, sticky = W)
        self.Neg1Button.config(height = 10, width = 10 )

        self.root.mainloop()

    def plus1(self):
        self.meal1 += 1
        self.DisplayButton["text"]=str(self.meal1)

    def neg1(self):
        self.meal1 -= 1
        self.DisplayButton["text"]=str(self.meal1)

if __name__ == "__main__":
    AppetiserClass()

我改动了不少内容。首先,你有很多代码写在任何特定方法之外,我更喜欢把这些代码放在类的方法里面,除了类变量的定义(比如meal1 = 0等等)。这其实是比较随意的——在方法中定义的self.whatever的可访问性和在类范围内定义的东西是一样的。我还让你可以通过self.ButtonName来保持对按钮的引用。最后,我让窗口只在你运行这个文件时实例化,而不是在将你的代码导入到其他文件时。

撰写回答