2017-11-27 9 views
-2

tkinter GUIの新機能です。私は、printというボタンにコマンドを割り当てる例を以下で試していました。私が間違っているつもりです任意のアイデアは、私はエラーを取得:Tkinterボタンへのコマンドの割り当て

_tkinter.TclError: bad option "-command": must be -after, -anchor, -before, -expand, -fill, -in, -ipadx, -ipady, -padx, -pady, or -side

Tkinterのインポートから*

class App: 
    def __init__(self, master): 
     fm = Frame(master) 
     Button(fm, text='Print').pack(side=TOP, anchor=W, fill=X, expand=YES, command = self.hello_world) 
     Button(fm, text='Center').pack(side=TOP, anchor=W, fill=X, expand=YES) 
     Button(fm, text='Bottom').pack(side=TOP, anchor=W, fill=X, expand=YES) 
     Button(fm, text='Left').pack(side=LEFT) 
     #Button(fm, text='This is the Center button').pack(side=LEFT) 
     Button(fm, text='Right').pack(side=RIGHT) 
     fm.pack(fill=BOTH, expand=YES) 

    def hello_world(self): 
     print ("Hello World") 

root = Tk() 
root.resizable(width=False, height=False) 
root.option_add('*font', ('verdana', 12, 'bold')) 
root.title("Pack - Example 12") 
display = App(root) 
root.mainloop() 
+1

エラーはわかりやすいものですが、 'command'は[widgetのオプション](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/button.html)です。 'Button'クラスの引数)であり、' pack'メソッドの引数ではありません。 – CommonSense

+1

'ボタン(...、コマンド= ...)' – furas

答えて

1

エラーは、あなたのコードが間違っているかを表示します。 commandpack(または他のジオメトリマネージャ)の有効なオプションではありません。コマンドは、ウィジェットのオプションを指定することで、ウィジェットのインスタンスを作成する際に使用する必要があります。

Button(fm, ..., command=...) 

私は、私は1つを追加した理由です、代わりにコメントで答えるの答えがあるはずだと思います。

+0

ええ、私はそれを理解しました。しかし、 –

+1

を確認してくれてありがとうございました。時にはソリューションがとてもシンプルです(退屈な、または1日で同じ説明を5回書いたくありません)ので、解説付きの書き込み回答より短いコメントを置く方が簡単です。そして、他の人はそれを完全な説明を加える回答として入れることができます。 – furas

+0

@furas私のサブノートが何か悪いように思えたら、すみません。私はちょうど私が2つの正しいコメントの後に長い答えを追加した理由について自分自身を説明しようとしていた。 – Lafexlos

関連する問題