Skip to content

Commit

Permalink
Added warning when app is already open
Browse files Browse the repository at this point in the history
  • Loading branch information
JRitmeester committed Aug 23, 2021
1 parent 4d31376 commit f984e92
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
13 changes: 6 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
1 change: 1 addition & 0 deletions tray_icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 15 additions & 2 deletions volume_thread.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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.")
Expand All @@ -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)
self.control.set_volume(values)

0 comments on commit f984e92

Please sign in to comment.