Tkinter:如何在关闭窗户之前做出动作?
嗨,
我正在尝试做一个小应用程序,使用Parralel端口远程控制音频放大器(开/关)。
我使用Tkinter对象来设置/重置并行端口的一条线路。
我的问题是找到一种方法,在Windows关闭时(当我切换计算机时)重置线路,以避免损坏我的扬声器。
有没有我可以利用的活动?类似于窗口顶部"X"上的鼠标循环事件(=ALT+F4)
我是一个初学者(2天),我发现很难找到我需要的信息(但Python的功能非常强大!)
谢谢你的帮助!
# 回答1
您可以使用包含代码的普通函数来关闭功放,然后退出。
您可以使用包含代码的普通函数来关闭功放,然后退出。
选择 | 换行 | 行号
- 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()
# 回答2
谢谢你这么快就回复了!
我对你的回答有两个问题:
-我找不到BusyBar(我使用的是Python2.7)...我在哪里可以下载?
-我的程序的目的是保护功放免受错误的影响,比如退出操作系统(Windows XP),忘记关闭它们。所以,我认为你的解决方案不符合我的需要。但是,我不能测试它(因为BusyBar)
还有.。也许我还没理解你的解决方案?
非常感谢你的帮助
跟Signal有关系吗?
我的节目:
谢谢你这么快就回复了!
我对你的回答有两个问题:
-我找不到BusyBar(我使用的是Python2.7)...我在哪里可以下载?
-我的程序的目的是保护功放免受错误的影响,比如退出操作系统(Windows XP),忘记关闭它们。所以,我认为你的解决方案不符合我的需要。但是,我不能测试它(因为BusyBar)
还有.。也许我还没理解你的解决方案?
非常感谢你的帮助
跟Signal有关系吗?
我的节目:
选择 | 换行 | 行号
- # -*- 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()
# 回答3
使用fin()执行您想要关闭功放的任何代码。代码应该在DESTORY()语句之前执行,因为这会关闭图形用户界面。
使用fin()执行您想要关闭功放的任何代码。代码应该在DESTORY()语句之前执行,因为这会关闭图形用户界面。
# 回答4
嗨,
通过使用os信号,我在以下方面取得了一些成功:
_
嗨,
通过使用os信号,我在以下方面取得了一些成功:
_
选择 | 换行 | 行号
- 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()
_
当我关闭窗口时,并行端口被重置,太棒了!
但当Windows(XP)关闭时,它不起作用...-(
我试过WM_KILL,但它不起作用。
您给我发了一个示例代码,我想看看它有什么作用,但我一直找不到BusyBar。我在哪里可以下载?
非常感谢,
西尔万
# 回答5
开始
这里
为百事可乐买的。
开始
这里
为百事可乐买的。
标签: python