2017-07-20 24 views
0

'Enter'キーを関数、具体的にはボタンとバインドする方法を知りません。 self.search関数で 'Enter'キーをバインドしたいと思います。私は以下のコードを持っていて、私はいろいろな方法で試しました。今すぐ入力ボックスをクリアするだけです。どんな助けでも大歓迎です。Python Tkinter GUIバインド 'Return' to function

class MainGUI: 
def __init__(self, master): 
    self.master = master 
    master.minsize(width=500, height=175) 
    master.title("Serial Number Decode") 

    self.label = Label(master, text="Serial Number Decoder") 
    self.label.pack() 
    self.textBox=Text(master, height=1, width=30) 
    self.textBox.place(relx=0.5, rely=0.1, anchor='n') 
    self.textBox2=Text(master, height=2, width=50,font=("Sans",12)) 

    self.textBox2.place(relx=0.5, rely=0.5, anchor='s') 
    self.search_button = Button(master, text="Search", command=self.search) 
    self.search_button.place(relx=0.75, rely=0.15, anchor='w') 

    #self.search_button.bind('<Return>', self.search) 

    self.master.bind('<Return>', self.search) #Just clears the entry box 

    self.multiLook_button = Button(master, text="MultiLook", command=self.multiLook) 
    self.multiLook_button.place(relx=0.7, rely=0.6, anchor='w') 

    self.multiSearch_button = Button(master, text="MultiSearch", command=self.multiSearch) 
    self.multiSearch_button.place(relx=0.84, rely=0.6, anchor='w') 

    self.close_button = Button(master, text="Close", command=master.quit) 
    self.close_button.place(relx=0.85, rely=0.15, anchor='w') 
+1

キーを正しくバインドしました。問題はself.search関数内にある必要があります。 – Novel

+0

あなたは正しいです。私は検索機能にイベント=なしを追加する必要がありました – mickNeill

答えて

3

あなたはそうのように、クラスで定義されsearchを持っていると仮定すると、正しい軌道に乗っている:あなたがそうであるようにあなたがメソッドにアクセスすることができます

class MainGUI(): 

    def __init__(self, master): 
     # ... Code ... 


    def search(self, event): 
     # ... Code ... 

self.search_button.bind('<Return>', self.search) 

この方法を使用すると、search_buttonウィジェットには、Enterキーを押したときにイベントバインディングがトリガーされるようにフォーカスが必要です。

さらに、アプリケーションに構造を加え、コードに読みやすさを加え、スケーリングを簡単にする方法を提案します。たとえば、将来アプリケーションに追加したい場合などです。 GUIを開発する際には体系的なアプローチやステップバイステップのモジュラリティが役立ちます。

import tkinter as tk 

class MainGUI(tk.Frame): 

    def __init__(self, master=None): 

     tk.Frame.__init__(self, master) 

     self.master.minsize(width=500, height=175) 
     self.master.title("Serial Number Decode") 

     self.main_label() 
     self.text_boxes() 
     self.buttons() 


    def main_label(self): 
     self.label = tk.Label(self, text='Serial Number Decoder') 
     self.label.pack() 


    def text_boxes(self): 
     # First we create the widgets 
     self.textBox = tk.Text(self, height=1, width=30) 
     self.textBox2 = tk.Text(self, height=2, width=50, font=("Sans",12)) 

     # Next place them within the GUI 
     self.textBox.place(relx=0.5, rely=0.1, anchor='n') 
     self.textBox2.place(relx=0.5, rely=0.5, anchor='s') 


    def buttons(self): 
     self.search_button = tk.Button(self, text="Search", command=self.search) 

     # You can set a widget to have the 'focus' like so: 
     self.search_button.focus() 

     # And bind an event on focus in, focus out, enter, shift, etc. - e.g.: 
     self.search_button.bind('<FocusIn>', self.do_something) 
     self.search_button.bind('<FocusOut>', self.do_something_else) 
     self.search_button.bind('<Return>', self.search) 

     # Other buttons below... 


    def search(self, event=None): 
     # Do something now that the event has been triggered 

注意するカップルの事 - def search(self, event=None)、私は(command=self.search設定から)ボタンがクリックされたのであれば、ここでイベントキーワード引数にNoneのデフォルト値を与える、「イベントは」ないはに渡されます。メソッドがバインディングからトリガーされた場合、 'イベント'が渡されます。また、私のコード例ではあまり徹底的ではなかったので、コードの部分を書いて構造化しました。 GUIに検索ボタンを登録していないので、表示されないか、self.do_somethingが定義されていないので、これを実行すると実際にAttributeErrorになります。私はこれがすべて役立つことを願っています最後に、tkinterのための素晴らしいリソース、NMT Tkinterがあります。

+0

構造および構造に関するヒントありがとうございました。私はそれを変更し、検索機能に 'event = None'を追加しました。現在は動作しています – mickNeill

+0

@PatrickONeill great!お役に立てて嬉しいです。 – flevinkelming