To detect if the close button is pressed in tkinter, use the following right before you run root.mainloop()
root.protocol("WM_DELETE_WINDOW", on_closing)
this will call the on_closing
function when the user presses the close button, which you can use the following code to make it into a button
root.iconify()
This will minimize it to the taskbar as a button.
Note this will not work on Mac as the window will just be hidden and you will have no way to actually closing it unless killing it manually
so something like this
from tkinter import tk
root = tk.Tk()
#define widgets and other things here
def on_closing():
root.iconify()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
If you want it to be on the system tray, tkinter have no support for that, so you'll have to use something like pystray