![](/img/trans.png)
[英]How can I enter data into database by pressing the button with using python?
[英]How can I bind pressing a button and hitting an enter key in python?
我想绑定一个按钮,然后按回车键。
我正在使用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()
我想让我的代码在按下按钮时以及在焦点位于按钮上时按下Enter键时都破坏窗口。
我该怎么做?
使用bind()
:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.