By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,034 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,034 IT Pros & Developers. It's quick & easy.

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

P: 3
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 !
Dec 14 '10 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 624
You would use a normal function that contains the code to shut down the amp and then quits.
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. import BusyBar
  3.  
  4.  
  5. class Application:
  6.     def __init__(self, root):
  7.         self.root = root
  8.         self.root.title('BusyBar Demo')
  9.         fr = Tkinter.Frame(self.root, width=300, height=100).pack()
  10.  
  11.         bb = BusyBar.BusyBar(fr, width=200)
  12.         bb.place(x=40, y=20)
  13.         bb.on()
  14.  
  15.         exit_button= Tkinter.Button(fr, text='EXIT',
  16.                    command=self.switch_off, bg='red', fg='white' )
  17.         exit_button.pack(fill=Tkinter.X, expand=1)
  18.  
  19.     def switch_off(self):
  20.         print "switching off the amp now"
  21.         self.root.quit()
  22.  
  23. if __name__ == '__main__':
  24.     root = Tkinter.Tk()
  25.     Application(root)
  26.     root.mainloop() 
Dec 15 '10 #2

P: 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 :

Expand|Select|Wrap|Line Numbers
  1. # -*- coding: cp1252 -*-
  2.  
  3. import parallel
  4. from Tkinter import *
  5.  
  6. def setvalue():
  7.     p.setData(1)
  8.  
  9. def resetvalue():
  10.     p.setData(0)
  11.  
  12. def fin():
  13.     fen1.destroy()
  14.  
  15. p=parallel.Parallel()
  16.  
  17. fen1= Tk()
  18. tex1 = Label(fen1, text='Commande du port parallèle', fg='red')
  19. tex1.pack()
  20. bou1 = Button(fen1, text='set', command = setvalue)
  21. bou1.pack()
  22. bou2 = Button(fen1, text='reset', command = resetvalue)
  23. bou2.pack()
  24. bou3 = Button(fen1, text='Quitter', command = fin)
  25. bou3.pack()
  26.  
  27. fen1.mainloop()
Dec 15 '10 #3

Expert 100+
P: 624
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.
Dec 15 '10 #4

P: 3
Hi,

I've had a little success, using the os signals, with this :
_______________________________________
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import parallel
  3.  
  4. root = Tk()
  5.  
  6. def Intercepte():
  7.     print "Interception de la fermeture de la fenetre"
  8.     resetvalue()
  9.     root.destroy()
  10.  
  11. p=parallel.Parallel()
  12.  
  13. def setvalue():
  14.     p.setData(1)
  15.  
  16. def resetvalue():
  17.     p.setData(0)
  18.  
  19. root.protocol("WM_DELETE_WINDOW", Intercepte)
  20. root.title("Commande du port parallele")
  21.  
  22.  
  23. tex1 = Label(root, text='Commande du port parallele', fg='red')
  24. tex1.pack()
  25. bou1 = Button(root, text='set', command = setvalue)
  26. bou1.pack()
  27. bou2 = Button(root, text='reset', command = resetvalue)
  28. bou2.pack()
  29.  
  30. 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
Dec 17 '10 #5

Expert 100+
P: 624
Start here for busybar.
Dec 17 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.