当前位置:   article > 正文

使用python进行简单的图形界面开发_python图形界面开发

python图形界面开发

1 处理的大体流程

2 使用PyQt5进行图形界面开发

2.1 入门参考链接

1)博客参考链接_01

2)博客参考链接_02

 

2.2 控件上添加执行动作

1)点击菜单栏上的“Edit Signal/Slot”按钮;

2)在要添加动作的控件上, 按住鼠标左键拖拽出如步骤2的图标;

3)放开鼠标左键, 弹出“Configure Connection”对话框;

4)根据步骤4, 选择控件的“激发动作的事件”;

5)根据步骤5,编辑一个函数名, 具体实现逻辑在python的调用代码实现;

6)根据步骤6, 选择要执行的函数;

7)点击“OK”确认动作;

 

2.3 美元兑换人民币demo

1 QtDesigner生成的图形界面代码

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'gu_test.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.13.0
  5. #
  6. # WARNING! All changes made in this file will be lost!
  7. from PyQt5 import QtCore, QtGui, QtWidgets
  8. class Ui_MainWindow(object):
  9. def setupUi(self, MainWindow):
  10. MainWindow.setObjectName("MainWindow")
  11. MainWindow.resize(1263, 1232)
  12. MainWindow.setWindowTitle("shy")
  13. self.centralwidget = QtWidgets.QWidget(MainWindow)
  14. self.centralwidget.setObjectName("centralwidget")
  15. self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
  16. self.lineEdit.setGeometry(QtCore.QRect(70, 80, 211, 41))
  17. self.lineEdit.setObjectName("lineEdit")
  18. self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
  19. self.textBrowser.setGeometry(QtCore.QRect(500, 80, 256, 41))
  20. self.textBrowser.setObjectName("textBrowser")
  21. self.label = QtWidgets.QLabel(self.centralwidget)
  22. self.label.setGeometry(QtCore.QRect(70, 60, 72, 15))
  23. self.label.setObjectName("label")
  24. self.label_2 = QtWidgets.QLabel(self.centralwidget)
  25. self.label_2.setGeometry(QtCore.QRect(500, 60, 72, 15))
  26. self.label_2.setObjectName("label_2")
  27. self.pushButton = QtWidgets.QPushButton(self.centralwidget)
  28. self.pushButton.setGeometry(QtCore.QRect(350, 90, 93, 28))
  29. self.pushButton.setAutoFillBackground(False)
  30. self.pushButton.setObjectName("pushButton")
  31. MainWindow.setCentralWidget(self.centralwidget)
  32. self.actionSave = QtWidgets.QAction(MainWindow)
  33. self.actionSave.setObjectName("actionSave")
  34. self.actionExit = QtWidgets.QAction(MainWindow)
  35. self.actionExit.setObjectName("actionExit")
  36. self.actionExit_2 = QtWidgets.QAction(MainWindow)
  37. self.actionExit_2.setObjectName("actionExit_2")
  38. self.actionHello1 = QtWidgets.QAction(MainWindow)
  39. self.actionHello1.setObjectName("actionHello1")
  40. self.actionHello2 = QtWidgets.QAction(MainWindow)
  41. self.actionHello2.setObjectName("actionHello2")
  42. self.retranslateUi(MainWindow)
  43. self.pushButton.clicked.connect(MainWindow.convert)
  44. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  45. MainWindow.setTabOrder(self.textBrowser, self.lineEdit)
  46. MainWindow.setTabOrder(self.lineEdit, self.pushButton)
  47. def retranslateUi(self, MainWindow):
  48. _translate = QtCore.QCoreApplication.translate
  49. self.label.setText(_translate("MainWindow", "美元"))
  50. self.label_2.setText(_translate("MainWindow", "人民币"))
  51. self.pushButton.setText(_translate("MainWindow", "转换"))
  52. self.actionSave.setText(_translate("MainWindow", "Save"))
  53. self.actionExit.setText(_translate("MainWindow", "Exit"))
  54. self.actionExit_2.setText(_translate("MainWindow", "Exit"))
  55. self.actionHello1.setText(_translate("MainWindow", "Hello1"))
  56. self.actionHello2.setText(_translate("MainWindow", "Hello2"))
'
运行

 

2 调用QtDesigner生成的图形界面代码

  1. import sys
  2. from PyQt5.QtWidgets import QApplication, QMainWindow
  3. from functools import partial
  4. from gu_test import * # 引入QtDesigner生成的模块
  5. # 逻辑封装类
  6. class MyWindow(QMainWindow, Ui_MainWindow):
  7. def __init__(self, parent=None):
  8. super(MyWindow, self).__init__(parent)
  9. self.setupUi(self)
  10. # 按钮的处理逻辑:1 获取输入信息;2 处理输入信息;3 展示结果信息
  11. def convert(self):
  12. input_content = self.lineEdit.text()
  13. result = float(input_content) * 6.7
  14. self.textBrowser.setText(str(result))
  15. # 主函数
  16. if __name__ == '__main__':
  17. app = QApplication(sys.argv)
  18. myWin = MyWindow()
  19. myWin.show()
  20. sys.exit(app.exec_())

3 效果图

4 打包成exe可执行文件

1)参考_01

2)参考_02

 

 

 

 

 

 

 

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

闽ICP备14008679号