如何在python中绑定按下按钮和按下enter键?

如何在python中绑定按下按钮和按下enter键?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我想绑定按下按钮和按下回车键 我正在使用Python 3 这是我的密码: class MakeaButton(object): def sizedButton(self, root, x, y): f = Frame(self.root, width = 100, height = 35) f.pack_propagate(0) f.place(x = x, y = y) self.btn = Button(f, text

我想绑定按下按钮和按下回车键

我正在使用Python 3

这是我的密码:

class MakeaButton(object):
    def sizedButton(self, root, x, y):
        f = Frame(self.root, width = 100, height = 35)
        f.pack_propagate(0)
        f.place(x = x, y = y)

        self.btn = Button(f, text = 'Button', command = self.destroy)
        self.btn.pack(fill = BOTH, expand = 1)

    def background(self):
        def close_onclick():
            sys.exit()

        self.root = Tk()
        self.root.geometry('1160x640')

        self.sizedButton(self.root, 530, 450)

        self.root.mainloop()

    def destroy(self):
        self.root.destroy()
我想让我的代码在按下按钮和按下进入键(焦点在按钮上)时破坏窗口

我怎么做?

使用
bind()

def sizedButton(self、root、x、y):
f=框架(self.root,宽度=100,高度=35)
f、 打包(0)
f、 位置(x=x,y=y)
self.btn=Button(f,text='Button',command=self.destroy)
self.btn.pack(填充=两者,扩展=1)
self.btn.bind(“,lambda e,b:self.destroy())#用于单击
self.btn.bind(“,lambda e,b:self.destroy())#返回

谢谢。但是出现了一个错误:TypeError:destroy()接受1个位置参数,但给出了2个。当我按下按钮时,错误显示出来,但窗口打开了。但是当我按下回车键时,错误就出来了,窗口也没有打开。@HoseongJeon:哦,对了。将其更改为
lambda e,b:self.destroy()
,因为回调会发送两个ArgumentHank。一开始它不正确,当我将“lambda e,b”改为“lambda e”时,它起作用了。非常感谢你
def sizedButton(self, root, x, y):
    f = Frame(self.root, width = 100, height = 35)
    f.pack_propagate(0)
    f.place(x = x, y = y)

    self.btn = Button(f, text = 'Button', command = self.destroy)
    self.btn.pack(fill = BOTH, expand = 1)
    self.btn.bind("<Button-1>", lambda e, b: self.destroy()) # for click
    self.btn.bind("<Return>", lambda e, b: self.destroy()) # for return