In this Video Lesson we show you how to create a Graphical User Interface to allow you to interact with your python program and hardware projects. The GUI can have buttons, slider bars, radio buttons, drop down menus, and many more widgets. In this lesson we show you how to install the package, and step-by-step instructions on how to use it. For your convenience, here is the code we developed in the lesson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt import time def greenButtonPressed(): print("green button clicked") def yellowButtonPressed(): print("yellow button clicked") def redButtonPressed(): print("red button clicked") def offButtonPressed(): print("off button clicked") def sliderValueChanged(val): frequency=val/10 sliderLabel.setText("Frequency: "+str(frequency)+" Hz") print("Frequency: ",frequency) app = QApplication(sys.argv) window = QWidget() window.setWindowTitle("My Grand Widget") window.setGeometry(100,100,800,600) widgetBox=QVBoxLayout(window) buttonBox=QHBoxLayout() greenButton = QPushButton("Green Button") greenButton.setStyleSheet("background-color: green; color: white;") greenButton.clicked.connect(greenButtonPressed) buttonBox.addWidget(greenButton) yellowButton = QPushButton("Yellow Button") yellowButton.setStyleSheet("background-color: yellow; color: black;") yellowButton.clicked.connect(yellowButtonPressed) buttonBox.addWidget(yellowButton) redButton = QPushButton("Red Button") redButton.setStyleSheet("background-color: red; color: white;") redButton.clicked.connect(redButtonPressed) buttonBox.addWidget(redButton) offButton = QPushButton("Off Button") offButton.setStyleSheet("background-color: black; color: white;") offButton.clicked.connect(offButtonPressed) slider = QSlider(Qt.Horizontal) slider.setMinimum(1) slider.setMaximum(40) slider.setValue(10) slider.setTickPosition(QSlider.TicksBelow) slider.setTickInterval(5) slider.valueChanged.connect(sliderValueChanged) sliderLabel=QLabel("Frequency: 1.0 HZ") sliderLabel.setAlignment(Qt.AlignCenter) sliderLabel.setStyleSheet("font-size: 24px;padding 2px;") widgetBox.addLayout(buttonBox) widgetBox.addWidget(sliderLabel) widgetBox.addWidget(slider) widgetBox.addStretch() window.setLayout(widgetBox) window.show() sys.exit(app.exec_()) |