Skip to content

Commit

Permalink
system tray icon fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
osmanonurkoc committed Feb 3, 2025
1 parent c2c9600 commit a60ad59
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions wallchanger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog, QSystemTrayIcon, QMenu, QAction, QCheckBox
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog, QSystemTrayIcon, QMenu, QAction, QCheckBox, QMessageBox

# Define the config file path in %APPDATA%\wallchanger
appdata_dir = os.path.join(os.getenv('APPDATA'), 'wallchanger')
Expand All @@ -26,7 +26,7 @@
if not os.path.exists(config_file):
with open(config_file, 'w') as f:
json.dump(default_config, f)
config = default_config
config = default_config
else:
try:
with open(config_file, 'r') as f:
Expand Down Expand Up @@ -95,8 +95,12 @@ def initUI(self):
layout.addWidget(save_button)

# System Tray setup
self.tray_icon = QSystemTrayIcon(QIcon("icon.ico"), self)
self.tray_icon.setVisible(True)
icon_path = os.path.abspath("icon.ico")
if not os.path.exists(icon_path):
QMessageBox.critical(self, "Error", "Missing icon.ico file!")
sys.exit(1) # Exit if the icon is missing

self.tray_icon = QSystemTrayIcon(QIcon(icon_path), self)
tray_menu = QMenu()
open_action = QAction('Open', self)
open_action.triggered.connect(self.show_window)
Expand All @@ -105,10 +109,11 @@ def initUI(self):
tray_menu.addAction(open_action)
tray_menu.addAction(exit_action)
self.tray_icon.setContextMenu(tray_menu)

# Close event handling
self.tray_icon.activated.connect(self.tray_icon_activated)

# Ensure the tray icon is visible
QTimer.singleShot(1000, lambda: self.tray_icon.setVisible(True))

self.setLayout(layout)

def browse_folder(self):
Expand All @@ -117,14 +122,17 @@ def browse_folder(self):
self.folder_entry.setText(folder)

def save_config(self):
config["wallpaper_folder"] = self.folder_entry.text()
config["change_interval"] = int(self.interval_entry.text())
config["run_at_startup"] = self.run_at_startup_checkbox.isChecked()
config["randomize_wallpapers"] = self.randomize_checkbox.isChecked()
with open(config_file, 'w') as f:
json.dump(config, f)
print(f"Configuration saved: {config}")
self.start_wallpaper_changer() # Restart the changer to apply new settings
try:
config["wallpaper_folder"] = self.folder_entry.text()
config["change_interval"] = int(self.interval_entry.text())
config["run_at_startup"] = self.run_at_startup_checkbox.isChecked()
config["randomize_wallpapers"] = self.randomize_checkbox.isChecked()
with open(config_file, 'w') as f:
json.dump(config, f)
print(f"Configuration saved: {config}")
self.start_wallpaper_changer() # Restart to apply new settings
except Exception as e:
QMessageBox.critical(self, "Error", f"Failed to save configuration: {e}")

def start_wallpaper_changer(self):
self.timer.stop()
Expand All @@ -137,10 +145,7 @@ def change_wallpaper(self):
if folder and os.path.exists(folder):
images = [f for f in os.listdir(folder) if f.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif'))]
if images:
if config.get("randomize_wallpapers", False):
image_path = os.path.join(folder, random.choice(images))
else:
image_path = os.path.join(folder, images[0]) # Pick the first wallpaper
image_path = os.path.join(folder, random.choice(images) if config.get("randomize_wallpapers", False) else images[0])
ctypes.windll.user32.SystemParametersInfoW(20, 0, image_path, 3)
print(f"Wallpaper changed to: {image_path}")
else:
Expand All @@ -151,23 +156,24 @@ def change_wallpaper(self):
def toggle_run_at_startup(self):
startup_path = os.path.join(os.getenv('APPDATA'), 'Microsoft\\Windows\\Start Menu\\Programs\\Startup', 'wallchanger.lnk')
if self.run_at_startup_checkbox.isChecked():
# Add shortcut to startup
self.create_startup_shortcut(startup_path)
else:
# Remove shortcut from startup
if os.path.exists(startup_path):
os.remove(startup_path)

def create_startup_shortcut(self, shortcut_path):
from win32com.client import Dispatch
script_path = os.path.abspath(sys.argv[0])
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortcut(shortcut_path)
shortcut.TargetPath = script_path
shortcut.WorkingDirectory = os.path.dirname(script_path)
shortcut.IconLocation = script_path
shortcut.save()
print(f"Startup shortcut created: {shortcut_path}")
try:
from win32com.client import Dispatch
script_path = os.path.abspath(sys.argv[0])
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortcut(shortcut_path)
shortcut.TargetPath = script_path
shortcut.WorkingDirectory = os.path.dirname(script_path)
shortcut.IconLocation = script_path
shortcut.save()
print(f"Startup shortcut created: {shortcut_path}")
except Exception as e:
QMessageBox.critical(self, "Error", f"Failed to create startup shortcut: {e}")

def tray_icon_activated(self, reason):
if reason == QSystemTrayIcon.Trigger:
Expand All @@ -186,6 +192,8 @@ def closeEvent(self, event):
self.hide()

if __name__ == '__main__':
app = QApplication([])
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False) # Ensure the app stays running
window = WallpaperChangerApp()
window.show()
sys.exit(app.exec_())

0 comments on commit a60ad59

Please # to comment.