Reputation: 3
I have a small script outputing serial commands via pyserial and tkinter.
Its all working well but just need to close and free up the serial port and wanted to use the "X" in the corner.
From what i understand this is a o/s "X" and not tkinter its self.
Can anyone point me in the right direction please?
Upvotes: 0
Views: 579
Reputation: 85613
You can use something like this
import Tkinter as tk
import tkMessageBox
def ask_quit():
if tkMessageBox.askokcancel("Quit", "You want to quit now? *sniff*"):
# close your serial here
root.destroy()
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", ask_quit)
root.mainloop()
Upvotes: 1
Reputation: 7367
Use the protocol method of Tk. If your window inherits from Tk you could do something like
self.protocol("WM_DELETE_WINDOW", self.exit)
in the __init__
method where self.exit is the function that closes resources.
If your window is a Tk object in its own right you could do something like
root = Tkinter.Tk()
root.protocol("WM_DELETE_WINDOW", <function to close resources>)
Upvotes: 1