From f984e929c92e19f66d3353721973d7fd47073452 Mon Sep 17 00:00:00 2001 From: JRitmeester Date: Mon, 23 Aug 2021 13:48:44 +0200 Subject: [PATCH] Added warning when app is already open --- main.py | 13 ++++++------- tray_icon.py | 1 + volume_thread.py | 17 +++++++++++++++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 2039e47..f2dd812 100644 --- a/main.py +++ b/main.py @@ -90,13 +90,12 @@ def except_hook(cls, exception, traceback): std_err_handler.err_msg.connect(tray_icon.std_err_post) # Errors that occur in the init phase aren't caught by the stderr. - tray_icon.show() - tray_icon.start_app() - # try: - # - # except Exception as e: - # QMessageBox.critical(None, "Error during start-up", f"An error occurred during start-up:\n\n{traceback.format_exc()}") - # logger.critical("Uncaught exception", exc_info=(type(e).__class__, e, e.__traceback__)) + try: + tray_icon.show() + tray_icon.start_app() + except Exception as e: + QMessageBox.critical(None, "Error during start-up", f"An error occurred during start-up:\n\n{traceback.format_exc()}") + logger.critical("Uncaught exception", exc_info=(type(e).__class__, e, e.__traceback__)) app.exec() print("This should not be printed.") diff --git a/tray_icon.py b/tray_icon.py index 99a5622..c657ebd 100644 --- a/tray_icon.py +++ b/tray_icon.py @@ -26,6 +26,7 @@ def __init__(self, icon, parent=None): self.setContextMenu(menu) self.activated.connect(self.onClick) + self.thread = VolumeThread() def std_err_post(self, msg): diff --git a/volume_thread.py b/volume_thread.py index da12a69..6e0893e 100644 --- a/volume_thread.py +++ b/volume_thread.py @@ -1,13 +1,17 @@ +import sys from pathlib import Path import serial from PyQt5.QtCore import QThread import logging +from PyQt5.QtWidgets import QMessageBox + from control import Control logger = logging.getLogger('root') + class VolumeThread(QThread): def __init__(self): @@ -16,7 +20,16 @@ def __init__(self): self.running = True self.control = Control(Path.cwd() / 'mapping.txt') logger.info("Setting up serial communication.") - self.arduino = serial.Serial(self.control.port, self.control.baudrate, timeout=.1) + try: + self.arduino = serial.Serial(self.control.port, self.control.baudrate, timeout=.1) + except serial.SerialException as e: + msg = QMessageBox.critical(None, "Application already running", + "The application crashed because the serial " + "connection is busy. This may mean that another " + "instance is already running. Please check the " + "system tray or the task manager.") + + raise def run(self): logger.info("Entering thread loop.") @@ -26,4 +39,4 @@ def run(self): data = str(self.arduino.readline()[:-2], 'utf-8') # Trim off '\r\n'. if data: values = [float(val) for val in data.split('|')] - self.control.set_volume(values) \ No newline at end of file + self.control.set_volume(values)