3

I made a GUI with Tkinter in python 3. Is it possible to close the window and have the application stays in the Windows System Tray?

Is there any lib or command within Tkinter for this.

Ethan Field
  • 4,646
  • 3
  • 22
  • 43
Ramon Basilio
  • 41
  • 1
  • 4
  • It's unclear what are you asking. Do you want - "When the close button is pressed the application will be minimized in System Tray instead of terminating"? – Partho63 May 13 '19 at 09:59
  • 1
    Maybe this can help you: https://stackoverflow.com/questions/4385656/tkinter-how-to-make-a-system-tray-application – Lndngr May 13 '19 at 10:16
  • 1
    I think I wasn't more clear! Excuse me! Parthon63 what I want is exactly it. When I closed the window it will be minimized in system tray. It there some way to do it? – Ramon Basilio May 13 '19 at 10:56
  • Again, it's unclear what you're asking. You want the window to be closed but still be open? Do you mean you want the "X" button to minimise instead of close? What have you tried? Where has your research led you? Do you have an example that isn't working as you'd expect? – Ethan Field May 13 '19 at 11:17

2 Answers2

3

The entire solution consists of two parts:

  1. hide/restore tkinter window
  2. create/delete systray object

Tkinter has no functionality to work with the system tray.
(root.iconify() minimizes to the taskbar, not to the tray)

step 1) (more info) can be done by

window = tk.Tk()
window.withdraw() # hide
window.deiconify() # show

step 2) can be done by site-packages, e.g. pystray

(an example, the same example, and more info)

2

You can use the wm_protocol specifically WM_DELETE_WINDOW protocol. It allows you to register a callback which will be called when the window is destroyed. This is an simple example:

import tkinter as tk

root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", root.iconify)
root.mainloop()

.iconify turns the window into an icon in System Tray.

Partho63
  • 3,117
  • 2
  • 21
  • 39