python 3.5.2发出错误'对象不带任何参数',我的代码有问题吗?我希望我的代码能运行,下面是我的代码

时间:2016-07-03 00:43:46

标签: python

from tkinter import*

class ListButtons(object):

    def _init_(self,master):
        frame = frame(master)
        frame.pack()
        self.printButton = Button(frame,text = 'print button',command = printMessage)

        self.printButton.pack( side = LEFT)
        self.quitButton =Button(frame,text = 'exit',command = frame.quit)
        self.printButton.pack( side = LEFT)
    def printMessage(self):
        print ('this actually worked')

root = Tk()

b = ListButtons(root)  #I get error object does not take any parameter#when I remove the root I get attribute error

root.mainloop()  

1 个答案:

答案 0 :(得分:1)

你缺少类的构造函数的双下划线,你的代码编译器认为它只是任何普通的方法(没有名称修改)。有关详细信息,请参阅此帖子:

What is the meaning of a single- and a double-underscore before an object name?

此外,您可以省略超类,因为默认情况下会继承object。框架需要大写,您需要自己引用打印消息,否则您将收到错误。这应该有效:

from Tkinter import*

class ListButtons:

    def __init__(self,master):
        frame = Frame(master)
        frame.pack()
        self.printButton = Button(frame,text = 'print button',command =
                              self.printMessage)


        self.printButton.pack( side = LEFT)
        self.quitButton =Button(frame,text = 'exit',command = frame.quit)
        self.printButton.pack( side = LEFT)

    def printMessage(self):
        print ('this actually worked')

root = Tk()

b = ListButtons(root) 

root.mainloop()