python - 怎么绑定(bind)escape键关闭这个窗口

标签 python python-2.7 tkinter

我拼凑了一个小程序来下载专利。我想将转义键绑定(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/

相关文章:

python - 如何将字典列表中的值与输入值进行比较

python - 在 Python 中实现自定义 Str 或 Buffer

python-3.x - 是否可以对 tkinter 进行多进程处理?

python - 'from modulename import function/variable' 是否将整个模块放入 sys 模块字典中?

python - 用于公开图像的 Django REST API

python - 获取字符串中集合中任意字符的第一次出现 - python

google-app-engine - 在同一个 webapp 中使用 flask 和 webapp2

python - 无法创建 tkinter 窗口

python - 列表框中的重复单词

python - 如何将字典值转换为字典集