4

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.

4
  • 1
    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
    Commented May 13, 2019 at 9:59
  • 1
    Maybe this can help you: stackoverflow.com/questions/4385656/…
    – Lndngr
    Commented May 13, 2019 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? Commented May 13, 2019 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? Commented May 13, 2019 at 11:17

2 Answers 2

5

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.

1
  • 4
    It doesn't add to tray... Just minimizes it Commented May 2, 2020 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.