Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythontkinterguibind';返回';发挥作用_Python_User Interface_Tkinter_Bind - Fatal编程技术网

pythontkinterguibind';返回';发挥作用

pythontkinterguibind';返回';发挥作用,python,user-interface,tkinter,bind,Python,User Interface,Tkinter,Bind,我很难弄清楚如何将“回车”键与函数绑定,或者更具体地说,与按钮绑定。我想用self.search函数绑定“Enter”键。我有下面的代码,我尝试了许多不同的方法。现在它只是清除了输入框。任何帮助都将不胜感激 class MainGUI: def __init__(self, master): self.master = master master.minsize(width=500, height=175) master.title("Serial Number Deco

我很难弄清楚如何将“回车”键与函数绑定,或者更具体地说,与按钮绑定。我想用self.search函数绑定“Enter”键。我有下面的代码,我尝试了许多不同的方法。现在它只是清除了输入框。任何帮助都将不胜感激

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')
class MainGUI:
定义初始(自我,主):
self.master=master
master.minsize(宽=500,高=175)
主标题(“序列号解码”)
self.label=标签(master,text=“序列号解码器”)
self.label.pack()
self.textBox=Text(主控,高度=1,宽度=30)
self.textBox.place(relx=0.5,rely=0.1,anchor='n')
self.textBox2=Text(主控,高度=2,宽度=50,字体=(“Sans”,12))
self.textBox2.place(relx=0.5,rely=0.5,anchor='s')
self.search\u按钮=按钮(master,text=“search”,command=self.search)
self.search_按钮.place(relx=0.75,rely=0.15,anchor='w')
#self.search_按钮绑定(“”,self.search)
self.master.bind(“”,self.search)#只是清除输入框
self.multiLook_按钮=按钮(master,text=“multiLook”,command=self.multiLook)
self.multiLook_按钮位置(relx=0.7,rely=0.6,anchor='w')
self.multiSearch_按钮=按钮(master,text=“multiSearch”,command=self.multiSearch)
self.multiSearch_按钮位置(relx=0.84,rely=0.6,anchor='w')
self.close\u按钮=按钮(master,text=“close”,command=master.quit)
self.close_按钮放置(relx=0.85,rely=0.15,anchor='w')

假设您在类中定义了
搜索
,那么您就在正确的轨道上了,如下所示:

class MainGUI():

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


    def search(self, event):
        # ... Code ...
您可以按如下方式访问该方法:

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

需要注意的几件事-
defsearch(self,event=None)
,我在这里给event关键字参数一个默认值
None
,因为如果单击按钮(从设置
command=self.search
),一个“event”将而不是传递给该方法,但是如果该方法是从绑定触发的,“事件”将被传递。另外,我在代码示例中也不是很透彻,我编写了几段代码,并对其进行了结构化,纯粹是为了举例,例如,我没有在GUI中注册搜索按钮,所以它不会出现,或者
self。dou\u有些东西没有定义
,所以运行此操作实际上会引发
AttributeError
。我希望这一切都有帮助!最后,这里是tkinter的一个很好的资源。

假设您在类中定义了
search
,那么您的思路是正确的,如下所示:

class MainGUI():

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


    def search(self, event):
        # ... Code ...
您可以按如下方式访问该方法:

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

需要注意的几件事-
defsearch(self,event=None)
,我在这里给event关键字参数一个默认值
None
,因为如果单击按钮(从设置
command=self.search
),一个“event”将而不是传递给该方法,但是如果该方法是从绑定触发的,“事件”将被传递。另外,我在代码示例中也不是很透彻,我编写了几段代码,并对其进行了结构化,纯粹是为了举例,例如,我没有在GUI中注册搜索按钮,所以它不会出现,或者
self。dou\u有些东西没有定义
,所以运行此操作实际上会引发
AttributeError
。我希望这一切都有帮助!最后,这里是tkinter的一个很好的资源。

您正确绑定了密钥。问题一定出在self.search函数中,您没有显示该函数。您是正确的。我需要将event=None添加到搜索函数中,以便正确绑定密钥。问题一定出在self.search函数中,您没有显示该函数。您是正确的。我需要将event=None添加到搜索功能中感谢您的帮助和有关结构的提示。我对其进行了修改,并将“event=None”添加到搜索功能中,现在可以使用了。感谢您的帮助和有关结构的提示。我对其进行了修改,并将“event=None”添加到搜索函数中,现在可以使用了