我拼凑了一个小程序来下载专利。我想将转义键绑定(bind)到关闭窗口的函数,但我真的不知道如何实现。我已经将转义键绑定(bind)到“退出”函数,但是有人可以帮我弄清楚如何编写关闭文本输入窗口的函数吗?
我是个菜鸟。
from Tkinter import *
import urllib
master = Tk()
e = Entry(master)
e.pack()
e.focus_set()
def patdload(self, event=None):
allnums = e.get()
index = 0
test = allnums.find('.')
if test > 0:
sep = 0
while sep != -1:
sep = allnums.find('.', index)
if sep != -1:
patno = allnums[index:sep]
elif sep == -1:
patno = allnums[index:]
#patno = e.get()
paturl = "http://patentimages.storage.googleapis.com/pdfs/US" + patno + ".pdf"
urllib.urlretrieve (paturl, (patno + ".pdf"))
index = sep + 1
else:
patno = e.get()
paturl = "http://patentimages.storage.googleapis.com/pdfs/US" + patno + ".pdf"
urllib.urlretrieve (paturl, (patno + ".pdf"))
def quit #help#:
master.bind('<Return>', patdload)
master.bind('<Escape>',quit)
#b = Button(master, text = "GET", width = 10, command = patdload)
#b.pack()
mainloop()
编辑:这是新的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
return self.func(*args)
File "C:\Python27\PatentGet.py", line 42, in <lambda>
master.bind('<Escape>', lambda x: close())
File "C:\Python27\PatentGet.py", line 39, in close
master.widthdraw() # if you want to bring it back
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1894, in __getattr__
return getattr(self.tk, attr)
AttributeError: widthdraw
最佳答案
首先,quit 是一个内置方法,所以我会使用另一个名称。否则,函数如下:
import sys
def close(event):
master.withdraw() # if you want to bring it back
sys.exit() # if you want to exit the entire thing
master.bind('<Escape>', close)
关于python - 怎么绑定(bind)escape键关闭这个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28467285/