-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_tray.py
More file actions
141 lines (119 loc) · 4.07 KB
/
Copy pathtest_tray.py
File metadata and controls
141 lines (119 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
Simple test script to verify system tray icon functionality
"""
import sys
import os
import webbrowser
from pathlib import Path
# Try to import required libraries
try:
import pystray
from PIL import Image, ImageDraw
print("✅ pystray and PIL imported successfully")
except ImportError as e:
print(f"❌ Import error: {e}")
print("Install with: pip install pystray pillow")
sys.exit(1)
def test_icon_creation():
"""Test creating a system tray icon"""
print("\n🔍 Testing icon creation...")
# Try to load icon.ico
icon_path = Path(__file__).parent / "icon.ico"
if icon_path.exists():
print(f"✅ Found icon.ico at: {icon_path}")
try:
image = Image.open(str(icon_path))
print(f"✅ Loaded icon.ico successfully (size: {image.size})")
except Exception as e:
print(f"⚠️ Could not load icon.ico: {e}")
image = create_fallback_icon()
else:
print(f"⚠️ icon.ico not found at: {icon_path}")
print(" Creating fallback icon...")
image = create_fallback_icon()
return image
def create_fallback_icon():
"""Create a simple fallback icon"""
image = Image.new('RGB', (64, 64), color=(33, 150, 243))
draw = ImageDraw.Draw(image)
draw.rectangle([10, 10, 54, 54], fill=(255, 255, 255))
draw.text((20, 20), "SS", fill=(33, 150, 243))
print("✅ Created fallback icon")
return image
def on_quit(icon, item):
"""Quit the application"""
print("\n👋 Exiting...")
icon.stop()
# Use os._exit to avoid SystemExit exception in callback
os._exit(0)
def on_show(icon, item):
"""Show a message"""
print("📌 Show clicked!")
def on_developer(icon, item):
"""Open developer website"""
print("📌 Opening developer website...")
try:
webbrowser.open("https://www.bibekchandsah.com.np/")
except Exception as e:
print(f"⚠️ Error: {e}")
def on_contribute(icon, item):
"""Open GitHub repository"""
print("📌 Opening GitHub repository...")
try:
webbrowser.open("https://github.com/bibekchandsah/screenshare")
except Exception as e:
print(f"⚠️ Error: {e}")
def test_menu_creation():
"""Test creating the menu"""
print("\n🔍 Testing menu creation...")
try:
menu = pystray.Menu(
pystray.MenuItem('Show', on_show, default=True),
pystray.Menu.SEPARATOR,
pystray.MenuItem('Developer', on_developer),
pystray.MenuItem('Contribute', on_contribute),
pystray.Menu.SEPARATOR,
pystray.MenuItem('Exit', on_quit)
)
print("✅ Menu created successfully")
return menu
except Exception as e:
print(f"❌ Error creating menu: {e}")
return None
def main():
"""Main test function"""
print("="*60)
print(" SYSTEM TRAY ICON TEST")
print("="*60)
# Test icon creation
image = test_icon_creation()
# Test menu creation
menu = test_menu_creation()
if not menu:
print("\n❌ Menu creation failed. Exiting.")
sys.exit(1)
# Create the tray icon
print("\n🔍 Creating system tray icon...")
try:
icon = pystray.Icon("TestTray", image, "Test System Tray", menu)
print("✅ System tray icon object created")
print("\n" + "="*60)
print("📌 Look for the tray icon in your taskbar!")
print(" • Right-click it to see the menu")
print(" • Click 'Exit' to close this test")
print("="*60 + "\n")
# Run the icon (this blocks until icon.stop() is called)
icon.run()
print("\n✅ Test completed successfully!")
except Exception as e:
print(f"\n❌ Error running tray icon: {e}")
print(f" Error type: {type(e).__name__}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n👋 Interrupted by user")
sys.exit(0)