2016-07-03 3 views
0
です。
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

2つのアンダースコアあなたのクラスのコンストラクタ、あなたのコードで、コンパイラはそれがちょうど普通の方法だと思っています(名前のマングリングはありません)。 objectはデフォルトで継承されるので、また、あなたはスーパークラスを残すことができます

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

:詳細はこの記事を参照してください。フレームを大文字にする必要があり、印刷メッセージを自己参照する必要があります。そうしないと、エラーが発生します。これはうまくいくはずです:

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() 
関連する問題