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

Commit

Permalink
Add toolbar with comport selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ijager committed Oct 30, 2019
1 parent 7671a02 commit bb3c30e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 30 deletions.
37 changes: 7 additions & 30 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from serial.tools import list_ports

from tools import set_background_color
from mainview import MainView

FPS = 20

Expand Down Expand Up @@ -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_())
43 changes: 43 additions & 0 deletions app/mainview.py
Original file line number Diff line number Diff line change
@@ -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))
55 changes: 55 additions & 0 deletions app/tools.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit bb3c30e

Please sign in to comment.