Bind Escape to Exit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Binny V A

    #1

    Bind Escape to Exit

    Hello Everyone,

    I am new to python and I am trying to get a program
    to close a application when the Escape Key is pressed.

    This is the code that I used

    ---------------------------------
    from Tkinter import *

    class Application(Fra me):
    def createWidgets(s elf):
    self.lab = Label(text="Hel lo World")
    self.lab.pack()

    def __init__(self, master=None):
    Frame.__init__( self, master)
    self.pack()
    self.createWidg ets()
    self.bind('<Key-Escape>',self.q uit)

    app = Application()
    app.mainloop()

    ---------------------------------

    It is displaying everything properly, but it is not quiting
    when the escape key is pressed.

    What am I doing wrong

    Thank You,
    Binny V A
    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

  • Harlin Seritt

    #2
    Re: Bind Escape to Exit

    Binny,

    The only way I could think to get this done was like so:

    from Tkinter import *

    class Application: # take away the inherited class

    def end(self, event):
    self.master.des troy()

    def createWidgets(s elf):
    self.lab = Label(text="Hel lo World")
    self.lab.pack()

    def __init__(self, master):
    self.master = master # Create a class version of master:
    self.master
    self.frame = Frame(self.mast er) # Change master to self.master
    self.frame.pack ()
    self.createWidg ets()
    self.master.bin d('<Escape>', self.end) # Change <Key-Escape> to
    <Escape>, link bind to self.end

    root = Tk() # Use a tk instance
    a = Application(roo t)
    root.mainloop() # Call tk.mainloop()

    I am almost certain that I had the same dilemma as you. Good luck. If
    you find a better way please post it.

    Thanks,

    Harlin Seritt

    Comment

    • Kent Johnson

      #3
      Re: Bind Escape to Exit

      Binny V A wrote:[color=blue]
      > Hello Everyone,
      >
      > I am new to python and I am trying to get a program
      > to close a application when the Escape Key is pressed.[/color]

      Here is a version that works. The changes from yours:
      - Bind <Escape>, not <Key-Escape>
      - Bind the key to the root, not the frame
      - Define a quit() method that takes an event parameter

      from Tkinter import *

      class Application(Fra me):
      def createWidgets(s elf):
      self.lab = Label(text="Hel lo World")
      self.lab.pack()

      def __init__(self, master=None):
      Frame.__init__( self, master)
      self.pack()
      self.createWidg ets()
      master.bind('<E scape>',self.qu it)

      def quit(self, event):
      Frame.quit(self )

      root=Tk()
      app = Application(roo t)
      app.mainloop()


      Kent
      [color=blue]
      >
      > This is the code that I used
      >
      > ---------------------------------
      > from Tkinter import *
      >
      > class Application(Fra me):
      > def createWidgets(s elf):
      > self.lab = Label(text="Hel lo World")
      > self.lab.pack()
      >
      > def __init__(self, master=None):
      > Frame.__init__( self, master)
      > self.pack()
      > self.createWidg ets()
      > self.bind('<Key-Escape>',self.q uit)
      >
      > app = Application()
      > app.mainloop()
      >
      > ---------------------------------
      >
      > It is displaying everything properly, but it is not quiting
      > when the escape key is pressed.[/color]

      Comment

      Working...