当前位置:   article > 正文

上位机图像处理和嵌入式模块部署(树莓派4b和pyqt5界面开发)_树莓派 pyqt5

树莓派 pyqt5

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

  

        在大部分linux程序开发中,一般是没有界面的。不过不排除有些场合,是需要用界面进行数据交互、调参使用的。这种情况下一般就用pyqt5做一个简单的界面就可以。特别是功能比较单一的时候,如果还要花大量的时间去做界面相关的工作,往往是得不偿失的。加之对于树莓派4b这样的界面,直接用mobaxterm可以直接进行界面开发,非常方便。

1、安装pyqt5

        在树莓派4b上面安装pyqt5,只需要一条命令就可以了,

sudo apt-get install python3-pyqt5

2、测试方法

        这个时候测试界面,很多同学希望可以有一个屏幕进行开发。其实这是不需要的。我们需要的只是一个mobaxterm软件。这样哪怕直接输入python demo.py也就是在pc电脑上显示出对应的效果。

3、第一个测试程序

        本着耳听为虚、眼见为实的原则,我们编写第一个程序demo.py程序,

  1. import sys
  2. from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
  3. class DemoApp(QWidget):
  4. def __init__(self):
  5. super().__init__()
  6. self.initUI()
  7. def initUI(self):
  8. self.setGeometry(100, 100, 300, 200) # Set window size and position
  9. self.setWindowTitle('PyQt5 Demo') # Set window title
  10. # Create a button
  11. self.button = QPushButton('Click Me', self)
  12. self.button.setGeometry(100, 50, 100, 30) # Set button size and position
  13. self.button.clicked.connect(self.showMessageBox) # Connect button click event to function
  14. def showMessageBox(self):
  15. # Show a message box when the button is clicked
  16. QMessageBox.information(self, 'Message', 'Button Clicked!')
  17. if __name__ == '__main__':
  18. # Create the PyQt5 application
  19. app = QApplication(sys.argv)
  20. # Create and show the demo application window
  21. demo = DemoApp()
  22. demo.show()
  23. # Run the application event loop
  24. sys.exit(app.exec_())

        程序比较简单,就是显示一个按钮,单击按钮后,有一个弹窗。效果如下所示,

4、第二个测试程序

        和第一个测试程序相比较,这一次多了一个菜单,我们来看看菜单时怎么做的,姑且暂时文件命名为menu.py,

  1. import sys
  2. from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QMenu
  3. class MenuDemo(QMainWindow):
  4. def __init__(self):
  5. super().__init__()
  6. self.initUI()
  7. def initUI(self):
  8. self.setWindowTitle('PyQt5 Menu Demo')
  9. self.setGeometry(100, 100, 400, 300)
  10. # Create actions for the File menu
  11. file_menu = self.menuBar().addMenu('File')
  12. new_action = QAction('New', self)
  13. new_action.triggered.connect(self.newFile)
  14. file_menu.addAction(new_action)
  15. exit_action = QAction('Exit', self)
  16. exit_action.triggered.connect(self.close)
  17. file_menu.addAction(exit_action)
  18. # Create actions for the Edit menu
  19. edit_menu = self.menuBar().addMenu('Edit')
  20. cut_action = QAction('Cut', self)
  21. cut_action.triggered.connect(self.cutText)
  22. edit_menu.addAction(cut_action)
  23. copy_action = QAction('Copy', self)
  24. copy_action.triggered.connect(self.copyText)
  25. edit_menu.addAction(copy_action)
  26. def newFile(self):
  27. print("New File created.")
  28. def cutText(self):
  29. print("Text cut.")
  30. def copyText(self):
  31. print("Text copied.")
  32. if __name__ == '__main__':
  33. app = QApplication(sys.argv)
  34. demo = MenuDemo()
  35. demo.show()
  36. sys.exit(app.exec_())

        代码执行后,效果是这样的,

5、第三个测试程序

        第三个测试是关于定时器的,就两个按钮,一个是开始,一个是结束。暂时把这个文件命名为timer.py,

  1. import sys
  2. from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton
  3. from PyQt5.QtCore import QTimer, QTime, Qt
  4. class TimerDemo(QWidget):
  5. def __init__(self):
  6. super().__init__()
  7. self.initUI()
  8. def initUI(self):
  9. self.setWindowTitle('PyQt5 Timer Demo')
  10. self.setGeometry(100, 100, 300, 200)
  11. self.label = QLabel('00:00:00', self)
  12. self.label.setAlignment(Qt.AlignCenter)
  13. self.start_button = QPushButton('Start', self)
  14. self.start_button.clicked.connect(self.startTimer)
  15. self.stop_button = QPushButton('Stop', self)
  16. self.stop_button.clicked.connect(self.stopTimer)
  17. layout = QVBoxLayout()
  18. layout.addWidget(self.label)
  19. layout.addWidget(self.start_button)
  20. layout.addWidget(self.stop_button)
  21. self.setLayout(layout)
  22. self.timer = QTimer()
  23. self.timer.timeout.connect(self.updateTime)
  24. self.elapsed_time = QTime()
  25. def startTimer(self):
  26. self.timer.start(1000) # Timer triggers every 1000ms (1 second)
  27. self.elapsed_time.start()
  28. def stopTimer(self):
  29. self.timer.stop()
  30. def updateTime(self):
  31. elapsed = int(self.elapsed_time.elapsed() / 1000) # Elapsed time in seconds
  32. time_display = QTime(0, 0).addSecs(elapsed).toString('hh:mm:ss')
  33. self.label.setText(time_display)
  34. if __name__ == '__main__':
  35. app = QApplication(sys.argv)
  36. demo = TimerDemo()
  37. demo.show()
  38. sys.exit(app.exec_())

        整个程序运行起来的效果是这样的,

6、总结

        如果我们开发的重点是算法和驱动程序,那么不需要在linux界面耽误太多的时间。或者说,即使是编写界面,也是在windows上位机上面开发界面,而不是现在的linux上面开发。

        当然,如果真的用pyqt5做最终的产品开发也是可以的。需要做的,就是美工、布局上优化一下,增加一下开机启动,并且做到全屏显示就可以了。实际上,pyqt5的功能还是非常多的,建议大家可以好好使用一下。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/870233
推荐阅读
相关标签
  

闽ICP备14008679号