【问题标题】:Tkinter - Python 3 - Adding an Increment Counter into a class window?Tkinter - Python 3 - 在类窗口中添加增量计数器?
【发布时间】:2014-10-31 21:48:47
【问题描述】:
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”上没有显示一个值,并且我收到这条消息: "NameError: 全局名称 'DisplayButton' 未定义"

“DisplayButton”,是我放置的一个按钮,用于显示一个值。仅此而已,但我收到此错误消息。

如果我删除类,然后只在单个窗口中运行这段代码,代码就可以正常工作。

任何帮助将不胜感激!

【问题讨论】:

    标签: python class tkinter counter increment


    【解决方案1】:

    如果您的缩进是正确的,那么问题不在于 DisplayButton 和 meal1 是全局的,而是它们是类级别的并且您没有以这种方式访问​​它,这意味着您应该使用 self 关键字来访问它。 (它不必是“self”——类中任何函数的第一个参数总是定义变量,您可以通过该变量访问同一类中的其他成员——但使用“self”是 Python 风格。)添加 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 继续引用您的按钮。最后,我已经做到了,只有在您运行文件而不是将代码导入其他文件时才实例化窗口。

    【讨论】:

    • @Pat6089 我已经在上面更新了它 - 一切正常,而且我已经做到了,这样你的班级成员之间的交流就容易多了。如果您有任何问题,请告诉我。
    • @pat6089 如果这有帮助,请不要忘记使用复选标记来选择此答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2019-02-25
    • 2014-10-30
    • 2016-05-10
    • 2019-01-06
    • 2017-03-28
    • 1970-01-01
    相关资源
    最近更新 更多