diff --git a/app/app.py b/app/app.py index 983c208..e087eea 100644 --- a/app/app.py +++ b/app/app.py @@ -7,6 +7,7 @@ from serial.tools import list_ports from tools import set_background_color +from mainview import MainView FPS = 20 @@ -75,45 +76,21 @@ def _update(self): return pass -class MyWidget(QtWidgets.QWidget): - def __init__(self): - 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(portsString) - self.text.setAlignment(QtCore.Qt.AlignCenter) - - self.layout = QtWidgets.QVBoxLayout() - self.layout.addWidget(self.text) - self.layout.addWidget(self.button) - self.setLayout(self.layout) - - self.button.clicked.connect(self.magic) - - - - - def magic(self): - self.text.setText(random.choice(self.hello)) - - if __name__ == "__main__": app = QtWidgets.QApplication([]) - widget = MyWidget() window = MainWindow() - window.setCentralWidget(widget) + window.show() signal.signal(signal.SIGINT, window.quit) # signal.signal(signal.SIGINT, signal.SIG_DFL) + toolbar = QtWidgets.QToolBar() + window.addToolBar(toolbar) + widget = MainView(toolbar) + window.setCentralWidget(widget) + if sys.flags.interactive != 1: sys.exit(app.exec_()) diff --git a/app/mainview.py b/app/mainview.py new file mode 100644 index 0000000..44c20b9 --- /dev/null +++ b/app/mainview.py @@ -0,0 +1,43 @@ +import random +from PySide2 import Qt, QtCore, QtWidgets, QtGui +import serial +from serial.tools import list_ports + +class MainView(QtWidgets.QWidget): + def __init__(self, toolbar): + super(MainView, self).__init__() + + availablePorts = list_ports.comports() + + portsString = ''.join([p.device + '\n' for p in availablePorts]) + + self.hello = ["Hallo Welt", "Hei maailma", "Hola Mundo", "Привет мир"] + + + self.toolbar = toolbar + + + self.button = QtWidgets.QPushButton("Click me!") + self.text = QtWidgets.QLabel("Text") + self.text.setAlignment(QtCore.Qt.AlignCenter) + + + + self.dropdown = QtWidgets.QComboBox() + self.dropdown.addItems([p.device for p in availablePorts]) + self.toolbar.addWidget(QtWidgets.QLabel("Select COM Port:")) + self.toolbar.addWidget(self.dropdown) + + + self.layout = QtWidgets.QVBoxLayout() + self.layout.addWidget(self.text) + self.layout.addWidget(self.button) + self.setLayout(self.layout) + + self.button.clicked.connect(self.magic) + + + + + def magic(self): + self.text.setText(random.choice(self.hello)) \ No newline at end of file diff --git a/app/tools.py b/app/tools.py new file mode 100644 index 0000000..7024620 --- /dev/null +++ b/app/tools.py @@ -0,0 +1,55 @@ +""" Some utility functions for PyQt widgets """ + +from PySide2.QtGui import QPalette, QColor +from PySide2 import QtWidgets + +def set_background_color(widget, color: str): + """ set widget color to color: str""" + pal = QPalette() + # set black background + pal.setColor(QPalette.Background, QColor(color)) + widget.setAutoFillBackground(True) + widget.setPalette(pal) + +def set_button_color(button, color: str): + """ set button color to color: str""" + pal = button.palette() + pal.setColor(QPalette.Button, QColor(color)) + button.setAutoFillBackground(True) + button.setPalette(pal) + button.update() + +def bold_label(text, size=13): + """ Create a label with font-weight bold """ + label = QtWidgets.QLabel(text) + label.setStyleSheet("font-weight: bold; font-size: {}px".format(size)) + label.update() + return label + +def spacer(): + """ Returns empty widget that expands to all available space """ + s = QtWidgets.QWidget() + s.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred) + return s + + + +class Row(QtWidgets.QWidget): + """Easily add widgets in a horizontal layout""" + + def __init__(self, *widgets): + super().__init__() + layout = QtWidgets.QHBoxLayout(self) + layout.setContentsMargins(0, 0, 0, 0) + for w in widgets: + layout.addWidget(w) + +class Column(QtWidgets.QWidget): + """Easily add widgets in a vertical layout""" + + def __init__(self, *widgets): + super().__init__() + layout = QtWidgets.QVBoxLayout(self) + layout.setContentsMargins(0, 0, 0, 0) + for w in widgets: + layout.addWidget(w)