stuart
stuart

Reputation: 3

tkinter "x" performing ser.close() on exit

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

Answers (2)

joaquin
joaquin

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

Holloway
Holloway

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

Related Questions