Control Panel
PyQt5
Qt is a widely used C++ framework for developing applications with rich graphical user interfaces, and PyQt5 is the Python version of the Qt framework, which can be used to create desktop applications.
Main Features
Cross-platform Compatibility: PyQt5 can run on multiple operating systems such as Windows, Mac OS, and Linux, allowing applications developed to have consistent performance and functionality on different platforms.
Rich Components: PyQt5 provides many standard GUI components such as buttons, dialogs, sliders, tables, and more complex components like calendars or browser engines.
Event Handling: In PyQt5, almost all events (such as keyboard keys, mouse clicks, etc.) are managed through an event handling mechanism, allowing developers to define how the application should respond to various events.
Support for Data Visualization and Graphics: PyQt5 can use tools like Qt Charts or QML to add charts or dynamic graphics to applications.
Modularity and Extensibility: PyQt5's design allows for the introduction of different modules as needed and supports extensions using C++ and other Qt languages.
Common Uses
- Desktop Software Development: Developing standalone desktop applications.
- Business Software: Creating business management systems, data analysis tools, etc.
- Education and Research Tools: Developing educational software or research data processing tools.
- Automation Scripts: Creating graphical user interfaces to control and automate script execution.
Installation Method
You can install PyQt5 using pip:
pip install PyQt5
This command will install PyQt5 and its dependencies for developing applications based on PyQt5.
Note
The Python environment of GNU Radio already includes the PyQt5 package, so there is no need to reinstall PyQt5 in the GNU Radio environment.
Control Panel
Below is an attempt to write a simple control panel using PyQt5. For explanations of the code, please refer to the explanatory text in the comments.
import sys
from PyQt5 import Qt
class Panel(Qt.QWidget):
stations = {
"Police Radio Station (94.3)": 94.3,
"Global Radio Station (96.7)": 96.7,
"Taipei Pop Radio Station (91.7)": 91.7,
}
def __init__(self):
super().__init__() # parent initialization
self.setWindowTitle("Radio Stations") # set title
vlayout = Qt.QVBoxLayout(self) # create a vertical layout
self.setLayout(vlayout) # set the layout
for name, freq in self.stations.items(): # loop items
btn = Qt.QRadioButton(name) # add a button
btn.toggled.connect(self.changeFreq) # connect click event
vlayout.addWidget(btn) # put into the vlayout
def changeFreq(self): # event handler
btn = self.sender() # get sender button
print(btn.text()) # print button text
print(self.stations[btn.text()]) # print frequency
if __name__ == "__main__":
# create application, sys.argv is command line arguments
app = Qt.QApplication(sys.argv)
panel = Panel() # create a panel instance
panel.show() # show the panel
sys.exit(app.exec_()) # start the event loop
In the above code, the app
object and the app.exec_()
function are mainly used to enter the event loop of the Qt program so that the panel
object can handle user-set click events.
Try running the above program, you should get the following result. Click on the buttons and observe the output in the terminal. You can also try resizing the window to observe layout changes.
About if __name__ == "__main__"
In Python, the purpose of if __name__ == "__main__":
line is to determine whether the code is being run directly or being imported into another Python program as a module. This mechanism is based on the __name__
variable in Python.
Purpose of __name__
Variable
When a Python script is run directly, the __name__
variable is set to "__main__"
. This means that if you run python script.py
in the command line, and script.py
contains if __name__ == "__main__":
, then the code inside this if
statement will be executed.
On the other hand, if the script is imported by another Python program as a module, for example using the import script
statement, the __name__
will be set to the name of that module (in this case, "script"
), and the code inside if __name__ == "__main__":
will not be executed.
This feature allows developers to have more flexible control over Python scripts, especially when writing reusable modules and test code. Developers can place test code or main program under this if
statement, so that this code will only run when the script is executed directly.
Exercise 3
Try modifying the above code by adding more radio stations or changing the layout. (AI assistance can be used)