[英]Python Tkinter GUI bind 'Return' to function
我在弄清楚如何將“ Enter”鍵與功能或更具體地說是按鈕綁定時遇到麻煩。 我想將'Enter'鍵與self.search函數綁定在一起。 我有下面的代碼,並且嘗試了許多不同的方法。 現在,它只是清除輸入框。 任何幫助將不勝感激。
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')
假設您已經在類中定義了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)
,這里給event關鍵字參數提供默認值None
因為如果單擊按鈕(從設置command=self.search
),“ event”將不會傳遞給該方法,但是如果該方法是從綁定觸發的,則將傳遞“事件”。 另外,我是不是在我的代碼示例很透徹,我寫的代碼段,並構造它,純粹是為了舉例的緣故,比如我沒有與GUI注冊搜索按鈕,這樣就不會出現,或self.do_something
沒有定義,因此運行它實際上會引發AttributeError
。 希望對您有幫助! 最后,這是tkinter的重要資源NMT Tkinter 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.