Tkinter:如何在关闭窗户之前做出动作?

嗨,
我正在尝试做一个小应用程序,使用Parralel端口远程控制音频放大器(开/关)。
我使用Tkinter对象来设置/重置并行端口的一条线路。
我的问题是找到一种方法,在Windows关闭时(当我切换计算机时)重置线路,以避免损坏我的扬声器。
有没有我可以利用的活动?类似于窗口顶部"X"上的鼠标循环事件(=ALT+F4)
我是一个初学者(2天),我发现很难找到我需要的信息(但Python的功能非常强大!)
谢谢你的帮助!

# 回答1


您可以使用包含代码的普通函数来关闭功放,然后退出。

选择 | 换行 | 行号
  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() 
# 回答2


谢谢你这么快就回复了!
我对你的回答有两个问题:
-我找不到BusyBar(我使用的是Python2.7)...我在哪里可以下载?
-我的程序的目的是保护功放免受错误的影响,比如退出操作系统(Windows XP),忘记关闭它们。所以,我认为你的解决方案不符合我的需要。但是,我不能测试它(因为BusyBar)
还有.。也许我还没理解你的解决方案?
非常感谢你的帮助
跟Signal有关系吗?
我的节目:

选择 | 换行 | 行号
  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()
# 回答3


使用fin()执行您想要关闭功放的任何代码。代码应该在DESTORY()语句之前执行,因为这会关闭图形用户界面。
# 回答4


嗨,
通过使用os信号,我在以下方面取得了一些成功:
_

选择 | 换行 | 行号
  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()

_
当我关闭窗口时,并行端口被重置,太棒了!
但当Windows(XP)关闭时,它不起作用...-(
我试过WM_KILL,但它不起作用。
您给我发了一个示例代码,我想看看它有什么作用,但我一直找不到BusyBar。我在哪里可以下载?
非常感谢,
西尔万

# 回答5


开始
这里
为百事可乐买的。

标签: python

添加新评论