Tkinter Python3在类窗口中添加增量计数器?

2024-10-03 04:39:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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按钮时,“DislpayButton”上没有显示一个值,我收到了以下消息: “名称错误:未定义全局名称'DisplayButton'”

“DisplayButton”,是我放置的一个按钮,用来显示一个值。没别的了,但我收到了这个错误消息。在

如果我删除这些类,只运行这段代码,只有一个窗口,代码可以正常工作。在

任何帮助都将不胜感激!在


Tags: textconfigappdefcolumnbuttonrootwidth
1条回答
网友
1楼 · 发布于 2024-10-03 04:39:06

如果你的缩进是正确的,问题不在于DisplayButton和meal1是全局的,而是它们是类级别的,而不是以这种方式访问它,这意味着您应该使用self关键字来访问它。(它不必是“self”—类中任何函数的第一个参数总是定义一个变量,通过该变量可以访问同一类中的其他成员—但使用“self”是Python风格的。)将self作为参数添加到该类中的所有函数中,如下所示:

def neg1(self):

然后通过self访问meal1和display按钮:

^{pr2}$

以及:

 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等),我更喜欢将这些代码保存在类方法内部。这是相当武断的-任何在方法中定义为自我。随便具有与在类作用域中定义的内容相同的可访问性。我也做了这样你就可以用自身按钮名称. 最后,我做了这样的设置:只有在运行文件而不将代码导入其他文件时,才会实例化窗口。在

相关问题 更多 >