> 文章列表 > GUI可视化应用开发及Python实现

GUI可视化应用开发及Python实现

GUI可视化应用开发及Python实现

0 建议学时

4学时,在机房进行

1 开发环境安装及配置

1.1 编程环境

安装PyCharm-community-2019.3.3
安装PyQt5

pip install PyQt5-tools -i https://pypi.douban.com/simple
pip3 install PyQt5designer -i https://pypi.douban.com/simple

1.2 环境配置

GUI可视化应用开发及Python实现
选择“Tools/External Tools”选项,单击添加“+”按钮,即可弹出外部工具配置窗口:
GUI可视化应用开发及Python实现
在弹出的外部工具配置窗口中,输入外部工具名称,这里用QtDesigner表示,接着输入Anaconda安装环境中的PyQt界面设计师可执行文件完整路径以及工作路径配置参数:
GUI可视化应用开发及Python实现
设置完后“Tools/ExternalTools”栏中增加了QtDesigner。
GUI可视化应用开发及Python实现
与上一节中配置QtDesigner外部工具操作一致,在“Tools/ExternalTools”中继续单击添加“+”按钮,命名为PyUCI
GUI可视化应用开发及Python实现

2 界面设计

在项目文件夹的Pycharm开发环境中,打开前面配置的外部工具QtDesigner:
GUI可视化应用开发及Python实现
GUI可视化应用开发及Python实现
GUI可视化应用开发及Python实现
GUI可视化应用开发及Python实现
GUI可视化应用开发及Python实现
右键ui界面文件,选择“Tools/ExternalTools/PyUCI”(前面配置的外部工具),即可自动生成Python程序代码:
GUI可视化应用开发及Python实现
类与对象:
GUI可视化应用开发及Python实现

class Y:def __init__(self):self.a = 0self.b = 0def value(self, t):return self.a + self.b + t
y = Y() #创建实例(对象)
y.a=1
y.b=2
v = y.value(2) #计算函数值

界面代码:

from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(591, 125)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.pushButton = QtWidgets.QPushButton(self.centralwidget)self.pushButton.setGeometry(QtCore.QRect(430, 20, 131, 41))font = QtGui.QFont()font.setFamily("微软雅黑")font.setPointSize(10)。。。self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)

3 程序逻辑编写

新建文件ff.py:

import sys
from mygui import *    #界面生成的python文件名
from PyQt5.QtWidgets import QApplication, QMainWindowclass MyMainWindow(QMainWindow, Ui_MainWindow):   #界面生成的python文件中类名def __init__(self, parent=None):super(MyMainWindow, self).__init__(parent)self.setupUi(self)self.pushButton.clicked.connect(self.pushButtonClicked)  #指定按键代码def pushButtonClicked(self):   #按键后执行的代码data1 = float(self.textEdit.toPlainText())data2 = float(self.textEdit_2.toPlainText())self.textEdit_3.setText(str(data1+data2))if __name__ == '__main__':app = QApplication(sys.argv)myWin = MyMainWindow() #新建一个窗口实例myWin.show()  #显示窗口sys.exit(app.exec_())

运行程序:
GUI可视化应用开发及Python实现
【例1】加法器
GUI可视化应用开发及Python实现

self.pushButton.clicked.connect(self.pushButtonClicked)def pushButtonClicked(self):   data1 = float(self.textEdit.toPlainText())data2 = float(self.textEdit_2.toPlainText())self.textEdit_3.setText(str(data1+data2))

【例2】计算器
GUI可视化应用开发及Python实现

self.pushButton_0.clicked.connect(self.pushButton0Clicked)
def pushButton0Clicked(self):    self.textEdit.setText(str(self.textEdit.toPlainText() + "0"))self.pushButton_add.clicked.connect(self.pushButtonAddClicked)
def pushButtonAddClicked(self):self.textEdit.setText(str(self.textEdit.toPlainText() + "+"))def pushButtonEqualClicked(self):try:self.textEdit.setText(str(eval(self.textEdit.toPlainText())))except:QMessageBox.critical(self, "Question", "表达式不正确!",QMessageBox.Ok, QMessageBox.Ok)

【例3】图片处理
GUI可视化应用开发及Python实现

from PyQt5.QtWidgets import QApplication, QMainWindow,QFileDialog
from PIL import Image
from PIL import ImageFilterself.path = ""self.pushButton_Open.clicked.connect(self.openimage)self.pushButton_GetBorder.clicked.connect(self.FindBorder)
def openimage(self):imgName, imgIType = QFileDialog.getOpenFileName(None,"导入图片","", "*.jpg;;*.png;;All Files(*)")jpg = QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height())self.label.setPixmap(jpg)self.path = imgName # self.textEdit.setText('')def FindBorder(self):if self.path!="" :im=Image.open(self.path)om=im.filter(ImageFilter.CONTOUR)om.save("1.jpg")jpg = QtGui.QPixmap("1.jpg").scaled(self.label_out.width(), self.label_out.height())    self.label_out.setPixmap(jpg)

【例4】蒙特卡罗求定积分
GUI可视化应用开发及Python实现

 self.pushButton_cal.clicked.connect(self.Calculate)
def MCint_area(self,f, a, b, n, fmax):below = 0for i in range(n):x = np.random.uniform(a, b)y = np.random.uniform(0, fmax)if 0 <= y <= f(x):below += 1area = below / n * (b - a) * fmaxreturn area
def f1(self,x):y=0y=eval(self.textEdit_f.toPlainText())return ydef Calculate(self):a=int(self.textEdit_a.toPlainText())b = int(self.textEdit_b.toPlainText())n=int(self.textEdit_Count.toPlainText())fmax = int(self.textEdit_fmax.toPlainText())area=0try:area=self.MCint_area(self.f1, a, b, n, fmax)except:QMessageBox.critical(self, "Question", "表达式不正确,无法计算!")self.textEdit_result.setText(str(area))

4 生成EXE

安装pyinstaller包
使用pip installa安装命令即可pyinstaller编译包,
也可以在Anaconda Prompt下实现安装。
GUI可视化应用开发及Python实现

pip install PyInstaller -i https://pypi.tuna.tsinghua.edu.cn/simple

GUI可视化应用开发及Python实现
编译不成功在项目文件夹下也会产生一个与项目名称相同的.spec文件
可用pycharm打开,查看问题
GUI可视化应用开发及Python实现

5 作业

制作一个与本专业有关的有界面程序,在窗口上写上姓名与学号。

五金工具网