Positional Argument TypeError python tkinter

时间:2015-03-27 22:56:56

标签: python python-3.x tkinter

我希望使用可点击按钮和键绑定返回运行相同的内容。这可能吗?我一直得到同样的错误,如下所示:

当我删除self.bind时,它会正常工作。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
TypeError: search_button() takes 1 positional argument but 2 were given

代码:

from tkinter import *

class Search(Tk):
    def __init__(self):
        Tk.__init__(self)

        self.bind("<Return>", self.search_button)

        self.search_bar = Frame(self, bg="blue")
        self.search_bar.pack(side=TOP, fill=X)
        self.index = Frame(self, width=100)
        self.index.pack(side=LEFT)
        self.content = Frame(self, bg="red")
        self.content.pack(side=LEFT, fill=X)
        self.status_bar = Frame(self, bg="green")
        self.status_bar.pack(side=BOTTOM, fill=X)

        self.entry = Entry(self.search_bar)
        self.entry.pack(side=LEFT, padx=4, pady=4)
        self.search = Button(self.search_bar, text="Search", command=self.search_button)
        self.search.pack(side=LEFT)
        self.content = Label(self.content, text="DUPA")
        self.content.pack(side=LEFT)
        self.status_bar = Label(self.status_bar, text="DUPA")
        self.status_bar.pack(side=LEFT)

    def search_button(self):
        (self.entry.get())
        if self.entry.get() == 'example1':
            print("lorem ipsum")

1 个答案:

答案 0 :(得分:4)

search_button()需要能够从绑定中获取event arg:

def search_button(self, event=None):
        (self.entry.get())
        if self.entry.get() == 'example1':
            print("lorem ipsum")