【问题标题】:How do I bind the escape key to close this window如何绑定转义键以关闭此窗口
【发布时间】:2015-04-12 14:06:01
【问题描述】:

我拼凑了一个小程序来下载专利。我想将转义键绑定到关闭窗口的函数,但我真的不知道如何实现这一点。我已将转义键绑定到“退出”功能,但有人可以帮我弄清楚如何编写关闭文本输入窗口的功能吗?

我是菜鸟。

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

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    您可以在 lambda 表达式中使用destroy() 方法来安全退出。我在python 3.7.x中使用过。

    import tkinter as tk
    root = tk.Tk()
    root.bind("<Escape>", lambda x: root.destroy())
    root.mainloop()
    

    【讨论】:

    • 问题已过时。因此,通过更新您的答案来添加您使用的版本来实现此功能。审查结束。
    【解决方案2】:

    您应该使用比sys.exit() 更安全的退出方法。在我的示例中,root.destroy() 是退出 Tkinter 应用程序的一种安全、可靠的方式。 destroy() 只是终止主循环并删除所有小部件。因此,如果您从另一个 Tkinter 应用程序调用您的应用程序,或者如果您有多个主循环,这似乎更安全。

    import tkinter as tk
    
    def exit(event):
        root.destroy()
    
    root = tk.Tk()
    
    root.bind("<Escape>", exit)
    root.mainloop()
    

    【讨论】:

      【解决方案3】:

      首先,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)
      

      【讨论】:

      • 谢谢 - 我试了一下,但现在我收到以下错误:Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“C:\Python27\lib\lib-tk \Tkinter.py",第 1532 行,在 call 中返回 self.func(*args) TypeError: close() 不接受任何参数(1 个给定)
      • 不,抱歉 - 它不起作用。我的问题是将错误复制并粘贴到评论中。错误在“第 1532 行”中。我编辑了第一条评论以包含错误文本。
      • 这里!你的想法是什么?
      • 我想通了。忘记了所有事件回调都需要一个参数。如果可以,标记为答案。这肯定会奏效。
      • 工作!非常感谢!
      猜你喜欢
      • 2018-06-05
      • 2011-12-03
      • 2020-01-15
      • 2018-02-07
      • 2020-12-06
      • 2021-08-29
      • 1970-01-01
      • 2021-04-08
      • 2012-11-01
      相关资源
      最近更新 更多