简体   繁体   中英

Tkinter escape binding not destroying

I am trying to learn tkinter and the idea that I have requires it to be in fullscreen. Before making it fullscreen, however I wanted to make sure I could close the window using escape. So through other questions similar to this one on Stack Overflow I have been trying to get it to destroy the tkinter window when I hit escape. To me this seems like it should work but I am getting an exception when I hit escape:

    `Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Jake\AppData\Local\Programs\Python\Python36-
32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
TypeError: destroy() takes 1 positional argument but 2 were given`

This is confusing for me because I don't think I am calling any arguments at all let alone 2. I have added a quit button which calls the close method I made and that works but using escape doesn't seem to. I have supplied my code for clarity. I know this is similar to a lot of questions on here but I have tried everything and nothing seems to be working for me. Thanks in advance!

import tkinter
from tkinter import *

class Window(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)

        master.bind('<Escape>', master.destroy)

        self.init_window()
    def init_window(self):
        self.pack(fill=BOTH, expand=1)
        quitButton = Button(self, text="quit", command=self.close)
        quitButton.place(x=0, y=0)

    def close(self):
        self.master.destroy()

def main():
    root = Tk()
    root.geometry('500x500')
    app = Window(root)
    root.mainloop()

main()

When you bind a function to an event, tkinter will always pass an event object to that function. The destroy function takes no arguments, which means you can't bind directly to it. You need to bind to a function that will accept the event object as an argument.

Since you already have a function, you can give it an optional named argument so that you can continue to call your function without the argument, but it can also be used in a binding:

class Window(Frame):
    def __init__(self, master = None):
        ...
        master.bind('<Escape>', self.close)

    def close(self, event=None):
        self.master.destroy()

You could also use an anonymous function:

master.bind('<Escape>', lambda event: master.destroy())

The following code works.
I used it in a class for creating a full screen app in a 800x480 touch screen for pi:

class FullScreenApp(object):

    def __init__(self, master, **kwargs):
        self.master=master
        pad=3
        self._geom='200x200+0+0'
        master.geometry('{}x{}'.format(800,480))
        master.bind('<Escape>', self.close)

    def close(self, event=None):
        self.master.destroy()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM