To modify your program such that it minimizes to the system tray when the minimize button is clicked and add a menu to the tray icon with custom actions, we can use the pystray library again.
First, install the pystray and Pillow libraries, if you haven't already:
pip install pystray pillow
Here's the modified version of your program:
import tkinter as tk
from tkinter import messagebox
from PIL import Image
import pystray
from pystray import MenuItem as item
# Define actions for the system tray menu
def action1(icon, item):
messagebox.showinfo("Action 1", "Action 1 was triggered!")
def action2(icon, item):
messagebox.showinfo("Action 2", "Action 2 was triggered!")
def show_window(icon, item):
icon.stop()
window.after(0, window.deiconify)
def hide_window():
window.withdraw()
icon.run()
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit the app?"):
icon.stop()
window.destroy()
# Create the main window
window = tk.Tk()
window.title("Fixed Size Window App")
window.geometry("300x200") # Fixed window size 300x200 pixels
# System tray icon setup
image_path = "icon.png" # Path to your icon image file
icon_image = Image.open(image_path)
menu = (item('Show', show_window),
item('Action 1', action1),
item('Action 2', action2),
item('Exit', on_closing))
icon = pystray.Icon("test_icon", icon_image, "My App", menu)
# Button click event handlers
def button1_clicked():
messagebox.showinfo("Button 1", "Button 1 was clicked!")
def button2_clicked():
messagebox.showinfo("Button 2", "Button 2 was clicked!")
def button3_clicked():
value = entry_field.get()
is_checked = checkbox_var.get()
messagebox.showinfo("Button 3",
f"Entry Value: {value}\nCheckbox is {'checked' if is_checked else 'not checked'}")
# Widgets setup
# ... (same as your original code)
# Disable resizing the window
window.resizable(False, False)
# Minimize the window
window.overrideredirect(True)
window.state('withdrawn')
# Override the default behavior when clicking the window's minimizing button
window.protocol("WM_DELETE_WINDOW", on_closing) # Calls on_closing when the window X button is clicked
# Initialize the system tray icon but don't show it yet
icon.run_detached()
# Start the GUI loop
window.mainloop()
A few things to note about the modifications:
- To minimize to the tray when the window minimize button is clicked, we've created an overrideredirect for the window and initially set the window state to withdrawn.
- The default X button behavior of the window has been changed to call the
on_closing function instead, using the window.protocol method.
- The
hide_window function is used to hide the main window and starts the tray icon when the application is minimized.
- The
show_window function stops the system tray icon and de-iconifies (shows) the main window.
- System tray menu items are defined and used to create the
pystray icon with a context menu that can perform custom actions. When the 'Exit' menu item is clicked, the on_closing function will be called which asks for user confirmation before quitting.
- Remember to replace
"icon.png" with the actual path to a valid image file that you want to use as your system tray icon.
- The app starts with the system tray icon running in detached mode, to avoid blocking the main thread.
Keep in mind that system tray interactions may vary between different operating systems, and this code is primarily tested on Windows. You should ensure compatibility with the target OS and handle OS-specific behaviors if necessary.