import tkinter as tk
from tkinter import messagebox
from PIL import Image
import pystray
from pystray import MenuItem as item
import threading
Function to exit the application from the system tray
def exit_app(icon, window):
icon.stop()
window.destroy()
Function to show the window from system tray
def show_window(icon, window):
icon.stop()
window.after(0, window.deiconify)
Function that will be called when the window close button is clicked
def on_close(window):
window.withdraw()
image = Image.open("icon.ico") # Your icon file path here
menu = (
item('Show', lambda icon, item: show_window(icon, window)),
item('Quit', lambda icon, item: exit_app(icon, window))
)
icon = pystray.Icon("test_icon", image, "System Tray App", menu)
icon.run(detach=False)
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'}")
Create the main window
window = tk.Tk()
window.title("Local Gaming Synchronizer")
window.geometry("400x200") # Fixed window size 300x200 pixels
Create labels
label1 = tk.Label(window, text="Label 1")
label1.pack()
Create a frame for the input field and Button 1
input_frame = tk.Frame(window)
input_frame.pack(pady=10)
Create an entry field
entry_field = tk.Entry(input_frame)
entry_field.grid(row=0, column=0, padx=5)
Create Button 1 and place it in line with the input field
button1 = tk.Button(input_frame, text="Button 1", command=button1_clicked)
button1.grid(row=0, column=1)
Create a checkbox
checkbox_var = tk.BooleanVar()
checkbox = tk.Checkbutton(window, text="Check me", variable=checkbox_var)
checkbox.pack()
label2 = tk.Label(window, text="Label 2")
label2.pack()
input2_frame = tk.Frame(window)
input2_frame.pack(pady=10)
Create Button 2 and Button 3
button2 = tk.Button(input2_frame, text="Button 2", command=button2_clicked)
button2.grid(row=0, column=0, padx=5)
button3 = tk.Button(input2_frame, text="Button 3", command=button3_clicked)
button3.grid(row=0, column=1)
Disable resizing the window
window.resizable(False, False)
Modify the behavior when the window close button is clicked
window.protocol("WM_DELETE_WINDOW", lambda: on_close(window))
Since we are now catching the window close event,
we no longer call window.mainloop() directly
threading.Thread(target=window.mainloop, daemon=True).start()
When I try to run it:
Exception in thread Thread-1 (mainloop):
Traceback (most recent call last):
Fatal Python error: _enter_buffered_busy: could not acquire lock for <_io.BufferedWriter name=''> at interpreter shutdown, possibly due to daemon threads
Python runtime state: finalizing (tstate=0x00007ff82bce3fe8)
Current thread 0x00000234 (most recent call first):
Extension modules: PIL._imaging (total: 1)