2017-08-29 26 views
-1

我有一段代码,一个饼干里面唱首歌,但是当我试图把它在一个类中这是行不通的,给我这个错误如何使一个按钮,一类

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() 
+0

我想你应该更多地考虑在你尝试在类中实现一个按钮之前,类是如何工作的(使用'__init__'方法,引用'self'来代替'global'等变量管理')。 – SneakyTurtle

+0

'命令=​​ self.click' –

+0

偷偷摸摸的乌龟,我告诉你,这就是我正在试图通过创建这个,而布赖恩,我试过了,但它给了我自己没有定义的错误 – Travis

回答

0

正如在评论中提到,你首先必须获得一个更好地了解如何班蟒蛇(和一般的)工作。

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

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() 

我希望这给你的,你需要做的意识,并了解更多的欲望。