Tkinter : how to make an action before closing the window ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Borenis
    New Member
    • Dec 2010
    • 3

    Tkinter : how to make an action before closing the window ?

    Hi,

    I'm trying to make a small application that uses parralel port for remote control of an audio amplifier (on/off).

    I use a Tkinter object to set/reset one of the line of the parallel port.

    My problem is to find a way to reset the line when windows closes (when I switch of the computer), to avoid damaging my loudspeakers.

    Is there an event that I could use ? Like the event of a mouse clic on the "X" on top of the window (=alt+F4)

    I'm a beginner (2 days), i find it difficult to find the informations I need (but Python is incredibly powerfull !)

    Thanks for your help !
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You would use a normal function that contains the code to shut down the amp and then quits.
    Code:
    import Tkinter
    import BusyBar
    
    
    class Application:
        def __init__(self, root):
            self.root = root
            self.root.title('BusyBar Demo')
            fr = Tkinter.Frame(self.root, width=300, height=100).pack()
    
            bb = BusyBar.BusyBar(fr, width=200)
            bb.place(x=40, y=20)
            bb.on()
    
            exit_button= Tkinter.Button(fr, text='EXIT',
                       command=self.switch_off, bg='red', fg='white' )
            exit_button.pack(fill=Tkinter.X, expand=1)
    
        def switch_off(self):
            print "switching off the amp now"
            self.root.quit()
    
    if __name__ == '__main__':
        root = Tkinter.Tk()
        Application(root)
        root.mainloop()

    Comment

    • Borenis
      New Member
      • Dec 2010
      • 3

      #3
      Thanks for answering so quickly !

      I have 2 problems with your answer :

      - I can't find BusyBar (i'm using Python 2.7)... Where can I download it?

      - the aim of my program is to protect the amp against mistakes, like exiting the os (windows xp), forgetting to switch them off. So, I don't think you're solution would match with my need. But, I cannot test it (because of BusyBar)

      And... Perhaps I'haven't understood your solution ?

      Thanks a lot for your help

      Is there anything to do with signal ?

      My program :

      Code:
      # -*- coding: cp1252 -*-
      
      import parallel
      from Tkinter import *
      
      def setvalue():
          p.setData(1)
          
      def resetvalue():
          p.setData(0)
      
      def fin():
          fen1.destroy()
      
      p=parallel.Parallel()
      
      fen1= Tk()
      tex1 = Label(fen1, text='Commande du port parallèle', fg='red')
      tex1.pack()
      bou1 = Button(fen1, text='set', command = setvalue)
      bou1.pack()
      bou2 = Button(fen1, text='reset', command = resetvalue)
      bou2.pack()
      bou3 = Button(fen1, text='Quitter', command = fin)
      bou3.pack()
      
      fen1.mainloop()
      Last edited by bvdet; Dec 20 '10, 04:59 PM. Reason: Add code tags

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Use fin() to execute what ever code you want to shut down the amp. The code should be executed before the destroy() statement though as that shuts down the GUI.

        Comment

        • Borenis
          New Member
          • Dec 2010
          • 3

          #5
          Hi,

          I've had a little success, using the os signals, with this :
          _______________ _______________ _________
          Code:
          from Tkinter import *
          import parallel
          
          root = Tk()
          
          def Intercepte():
              print "Interception de la fermeture de la fenetre"
              resetvalue()
              root.destroy()
          
          p=parallel.Parallel()
              
          def setvalue():
              p.setData(1)
              
          def resetvalue():
              p.setData(0)
          
          root.protocol("WM_DELETE_WINDOW", Intercepte)
          root.title("Commande du port parallele")
          
          
          tex1 = Label(root, text='Commande du port parallele', fg='red')
          tex1.pack()
          bou1 = Button(root, text='set', command = setvalue)
          bou1.pack()
          bou2 = Button(root, text='reset', command = resetvalue)
          bou2.pack()
          
          root.mainloop()
          _______________ _______________ _____

          When I close the window, the parallel port is resetted, great !
          But it doesn't work when Windows (XP) closes... :-(

          I've tried WM_KILL, but it doesn't work.

          You've sent me a sample code, I'd like to see what it does, but I've been unable to find BusyBar. Where can I download it ?

          Thanks a lot,

          Sylvain
          Last edited by bvdet; Dec 20 '10, 05:00 PM. Reason: Add code tags

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            Start here for busybar.

            Comment

            Working...