Python 3.x 如何在类中制作按钮

Python 3.x 如何在类中制作按钮,python-3.x,tkinter,Python 3.x,Tkinter,我有一个cookieclicker的代码块,但是当我试图将它放入一个类中时,它不起作用,给了我这个错误 Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Travi\AppData\Local\Programs\Python\Python36- 32\lib\tkinter\__init__.py", line 1699, in __call__ return self.fu

我有一个cookieclicker的代码块,但是当我试图将它放入一个类中时,它不起作用,给了我这个错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Travi\AppData\Local\Programs\Python\Python36-
32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
TypeError: click() missing 1 required positional argument: 'self'
我不明白为什么它不起作用 我的代码如下

from tkinter import*
import os
root = Tk()
class Main():
    os.chdir('C:\\Users\\Travi\\Downloads')
    cookies = 0
    grandmas =0
    gmaprice = 10

    cookiesIcon = Label(root,text = "Cookies you have:"+str(cookies))
    cookiesIcon.grid(row = 1,column = 0)
    gma = Label(root,text = "Grandmas you have:"+str(grandmas))
    gma.grid(row = 0, column = 1)
    def click(self):
        global cookies,cookiesIcon
        cookies+=1
        cookiesIcon.config(text = "Cookies you have:"+str(cookies))
    def grandma(self):
        global cookies,grandmas,cookiesIcon,gma,gmaprice
        if cookies>gmaprice:
            grandmas+=1
            cookies-=gmaprice
            gmaprice+=5
            cookies.config(text = "Cookies you have:"+str(cookies))
            gma.config(text = "Grandmas you have:"+str(grandmas))
    photo=PhotoImage(file = "Cookies.gif")
    b = Button(root,command =click)
    b.config(image=photo)
    b.grid()
    gmaupgrade=Button(root,command =grandma,text = "Grandmas for sale")
    gmaupgrade.grid(column = 1, row = 1)
    root.mainloop()

正如在评论中提到的,您必须首先更好地理解python中的类(以及一般情况下)是如何工作的

我已经纠正了您代码中的一系列错误;即:
-添加一个``函数来初始化类属性。
-删除了不有用的
global
语句;所有类属性都可以在类中访问。
-更正了一些错误使用的变量(
cookies
i/o
cookiesIcon
Grand
i/o
gma


我希望这能让你明白你需要做什么,并渴望了解更多

我认为在尝试在类中实现按钮之前,您应该更多地了解类是如何工作的(使用
\uuuu init\uuuu
方法,引用
self
进行变量管理,而不是
global
等)。
command=self。单击
skilly-Turtle,我对您说,这就是我试图通过创建它所做的,布莱恩,我试过了,但它给了我一个错误,自我没有定义对不起,我没有看到另一个非常明显的错误。您需要将此代码放入
\uuuuu init\uuuuu
中。好的,我尝试将其放在两个不同的位置,但他们做了两件事之一,没有显示任何屏幕,或者没有显示按钮
from tkinter import*
import os
root = Tk()

class Main():

    def __init__(self):
#         os.chdir('C:\\Users\\Travi\\Downloads')
        self.cookies = 0
        self.grandmas =0
        self.gmaprice = 10

        self.cookiesIcon = Label(root,text = "Cookies you have:"+str(self.cookies))
        self.cookiesIcon.grid(row=1, column=0)
        self.gma = Label(root,text = "Grandmas you have:"+str(self.grandmas))
        self.gma.grid(row = 0, column = 1)

#        photo=PhotoImage(file = "Cookies.gif")
        b = Button(root, command=self.click)
#        b.config(image=photo)
        b.grid()

        self.gmaupgrade=Button(root,command=self.grandma, text = "Grandmas for sale")
        self.gmaupgrade.grid(column = 1, row = 1)
        root.mainloop()

    def click(self):
        self.cookies += 1
        self.cookiesIcon.config(text = "Cookies you have:"+str(self.cookies))

    def grandma(self):
        if self.cookies > self.gmaprice:
            self.grandmas += 1
            self.cookies -= self.gmaprice
            self.gmaprice += 5
            self.cookiesIcon.config(text="Cookies you have:" + str(self.cookies))
            self.gma.config(text="Grandmas you have:" + str(self.grandmas))


Main()