Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Create QtMainwindow with ctrl-c and cmd-q exit signals
Browse files Browse the repository at this point in the history
  • Loading branch information
ijager committed Oct 30, 2019
1 parent 621e016 commit 7671a02
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ verify_ssl = true

[packages]
pyside2 = "*"
pyserial = "*"

[requires]
python_version = "3.7"
10 changes: 9 additions & 1 deletion app/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 91 additions & 6 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,92 @@
import sys
import random
from PySide2 import QtCore, QtWidgets, QtGui
import signal
from PySide2 import Qt, QtCore, QtWidgets, QtGui

import serial
from serial.tools import list_ports

from tools import set_background_color

FPS = 20

class MainWindow(QtWidgets.QMainWindow):
"""
Class docstring
"""

def __init__(self):
super(MainWindow, self).__init__()

self._running = True
exitShortcut = QtWidgets.QShortcut("CTRL+Q", self)
exitShortcut.activated.connect(self.close) # calls closeEvent

self._setupView()

self.timer = QtCore.QTimer()
self.timer.timeout.connect(self._update)
self.timer.start(1000/FPS)

self.popup = None

def _setupView(self):
"""Initialize Main Window"""
# self.setWindowIcon(QtGui.QIcon('assets/icon.png'))
self.setGeometry(50, 50, 1600, 900)
self.setWindowTitle("Serial Console")
set_background_color(self, 'white')

# self.setCentralWidget(self.terminal.view)
# self._center()
# self.raise_()
# self.activateWindow()

def closeEvent(self, event):
"""Handle window close event"""
if event:
# self.terminal.close()
event.accept()
if not self._running:
return
print('shutting down..')
self._running = False

def quit(self, _signal=None, _=None):
""" Signal Handler to quit the program """
self.close()

def _error_popup(self, msg):
QtWidgets.QMessageBox.critical(self, "Unexpected Error", msg)

def _center(self):
""" Center Window on current display """
frameGm = self.frameGeometry()
screen = QtWidgets.QApplication.desktop().screenNumber(QtWidgets.QApplication.desktop().cursor().pos())
centerPoint = QtWidgets.QApplication.desktop().screenGeometry(screen).center()
frameGm.moveCenter(centerPoint)
self.move(frameGm.topLeft())

def _update(self):
""" Gui Thread poll """
if not self._running:
# not running, pass update
self.close()
return
pass

class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
super(MyWidget, self).__init__()

availablePorts = list_ports.comports()

portsString = ''.join([p.device + '\n' for p in availablePorts])

self.hello = ["Hallo Welt", "Hei maailma", "Hola Mundo", "Привет мир"]

self.button = QtWidgets.QPushButton("Click me!")
self.text = QtWidgets.QLabel("Hello World")
self.text = QtWidgets.QLabel(portsString)
self.text.setAlignment(QtCore.Qt.AlignCenter)

self.layout = QtWidgets.QVBoxLayout()
Expand All @@ -20,6 +97,8 @@ def __init__(self):
self.button.clicked.connect(self.magic)




def magic(self):
self.text.setText(random.choice(self.hello))

Expand All @@ -28,7 +107,13 @@ def magic(self):
app = QtWidgets.QApplication([])

widget = MyWidget()
widget.resize(800, 600)
widget.show()

sys.exit(app.exec_())
window = MainWindow()
window.setCentralWidget(widget)

window.show()
signal.signal(signal.SIGINT, window.quit)
# signal.signal(signal.SIGINT, signal.SIG_DFL)

if sys.flags.interactive != 1:
sys.exit(app.exec_())

0 comments on commit 7671a02

Please sign in to comment.