2017-10-07 1 views
0

ButtonをOPButtunにサブクラス化したいと思います。 OPButtonは、マウスがホバリングしているときにヘルプメッセージを書き込むことができる通常のボタンです。 OPButtonはTHRE、通常のボタンのコンストラクタが受け入れるであろうと考えられるあらゆるパラメータリストを受け付け、プラス自分の2する必要があります。それを書き込むためのメッセージとSTRINGVARTkinter:追加の引数を持つボタンをサブクラス化する

これは私のコード(おそらく実行可能)

from tkinter import * 
from tkinter import ttk 

class OPButton(Button): 
    """ """ 
    def ___init___(self, parent, string, message, *args, **kwargs): 
     ttk.Button.__init__(self, parent, *args, **kwargs) 
     self.bind("<Enter>", command=lambda e: string.set(message)) 
     self.bind("<Leave>", command=lambda e: string.set("")) 

if __name__ == '__main__': 
    root = Tk() 
    root.str= StringVar() 
    OPButton(root, root.str, "hovering the button", text="click here").pack() 
    ttk.Label(root, textvariable=root.str).pack() 
    root.mainloop() 
です

と、エラーメッセージ:

Traceback (most recent call last): 
    File "C:\Users\planchoo\oPButton.py", line 19, in <module> 
    OPButton(root, "Hello World", "Bouton", text="Hello").pack() 
TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given 

編集:以下はブライアンの応答後に修正されたコードです。完璧に働く(ありがとう)。

from tkinter import * 
from tkinter import ttk 

class OPButton(Button): 
    """ """ 
    def __init__(self, parent, string, message, *args, **kwargs): 
     ttk.Button.__init__(self, parent, *args, **kwargs) 
     self.bind("<Enter>", lambda e: string.set(message)) 
     self.bind("<Leave>", lambda e: string.set("")) 

if __name__ == '__main__': 
    root = Tk() 
    root.chaine = StringVar() 
    OPButton(root, root.chaine, "Bouton", text="Hello").pack() 
    ttk.Label(root, textvariable=root.chaine).pack() 
    root.mainloop() 

答えて

1

私はあなたが定義した__init__()関数は___init___()として綴られたかなり確信しています。

+0

ありがとうございました。私は正しい方向に見ていませんでした。私は元の投稿の最後にデバッグされたコードを投稿しました。再度、感謝します。間違いはちょっとタイプミスではありましたが、何時間も悩んでいました。 – quickbug

+0

@quickbug問題ありません。 :) –

関連する問題