赞
踩
利用Qt5design或Qt5Creator制作界面的布局,包括设置按钮的个数、布局和大小等,注意记得为每个按钮添加槽函数。保存生成.ui文件。
利用pyuic5 xxx.ui >xxx.py或pyuic5 xxx.ui -o xxx.py命令生成.py文件如下:
- # -*- coding: utf-8 -*-
-
- # Form implementation generated from reading ui file 'mainwindow.ui'
- #
- # Created by: PyQt5 UI code generator 5.9.2
- #
- # WARNING! All changes made in this file will be lost!
-
- from PyQt5 import QtCore, QtGui, QtWidgets
-
- class Ui_MainWindow(object):
- def setupUi(self, MainWindow):
- MainWindow.setObjectName("MainWindow")
- MainWindow.resize(800, 600) #界面大小
- self.centralwidget = QtWidgets.QWidget(MainWindow)
- self.centralwidget.setObjectName("centralwidget")
- self.pushButton = QtWidgets.QPushButton(self.centralwidget)
- self.pushButton.setGeometry(QtCore.QRect(175, 75, 150, 50)) #按钮(x,y,w,h)
- self.pushButton.setObjectName("pushButton")
- self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
- self.pushButton_2.setGeometry(QtCore.QRect(175, 275, 150, 50))
- self.pushButton_2.setObjectName("pushButton_2")
- self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
- self.pushButton_3.setGeometry(QtCore.QRect(175, 475, 150, 50))
- self.pushButton_3.setObjectName("pushButton_3")
- self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
- self.pushButton_4.setGeometry(QtCore.QRect(475, 75, 150, 50))
- self.pushButton_4.setObjectName("pushButton_4")
- #self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
- #self.pushButton_5.setGeometry(QtCore.QRect(340, 90, 89, 25))
- #self.pushButton_5.setObjectName("pushButton_5")
- self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
- self.pushButton_6.setGeometry(QtCore.QRect(475, 475, 150, 50))
- self.pushButton_6.setObjectName("pushButton_6")
- '''MainWindow.setCentralWidget(self.centralwidget)
- self.menubar = QtWidgets.QMenuBar(MainWindow)
- self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
- self.menubar.setObjectName("menubar")
- MainWindow.setMenuBar(self.menubar)
- self.statusbar = QtWidgets.QStatusBar(MainWindow)
- self.statusbar.setObjectName("statusbar")
- MainWindow.setStatusBar(self.statusbar)'''
-
- self.retranslateUi(MainWindow)
- self.pushButton.clicked.connect(MainWindow.loadimg) #loadimg为槽函数名
- self.pushButton_2.clicked.connect(MainWindow.tag)
- self.pushButton_3.clicked.connect(MainWindow.cal)
- self.pushButton_4.clicked.connect(MainWindow.reset)
- #self.pushButton_5.clicked.connect(MainWindow.resetall)
- self.pushButton_6.clicked.connect(MainWindow.calall)
- QtCore.QMetaObject.connectSlotsByName(MainWindow)
-
- def retranslateUi(self, MainWindow):
- _translate = QtCore.QCoreApplication.translate
- MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
- self.pushButton.setText(_translate("MainWindow", "载入图片"))
- self.pushButton_2.setText(_translate("MainWindow", "标注"))
- self.pushButton_3.setText(_translate("MainWindow", "计算当前"))
- self.pushButton_4.setText(_translate("MainWindow", "重置当前"))
- #self.pushButton_5.setText(_translate("MainWindow", "全部重置"))
- self.pushButton_6.setText(_translate("MainWindow", "计算均值"))
如果后续需要对界面进行改动,可以直接在这个.py进行修改。
新建一个.py文件,写入如下代码:
- # -*- coding: utf-8 -*-
-
- from PyQt5 import QtWidgets, QtGui
- import sys
- from PIL import Image
- import cv2
- import numpy as np
- import pylab as pl
- import matplotlib.pyplot as plt
- import scipy.io as sio
- from PIL import Image
- import os
-
- from mainwindow import Ui_MainWindow # 导入生成mainwindow.py里生成的类
-
- class mywindow(QtWidgets.QWidget,Ui_MainWindow):
-
-
- def __init__(self):
- super(mywindow,self).__init__()
- self.setupUi(self)
-
- self.num=0 #image number
- self.count=0
-
-
-
-
-
- #定义槽函数
- def loadimg(self):
- if self.count==0:
- print("start loading image!")
-
- else:
- print('please execute in order!')
-
- def tag(self):
- if self.count==1:
- print("start tagging!")
-
- else:
- print('please execute in order!')
-
-
- def cal(self):
- if self.count==2:
- print("start calculating!")
-
- else:
- print('please execute in order!')
-
- def reset(self):
- print("start with tagging!")
- self.count=1
-
- '''def resetall(self):
- print("resetting all!")'''
-
-
- def calall(self):
- if self.num==10:
- print("start calculating all!")
-
- else:
- print('please execute in order!')
-
-
-
- app = QtWidgets.QApplication(sys.argv)
- window = mywindow()
- window.show()
- sys.exit(app.exec_())
-
可以在类中写入自己需要使用的函数,每个槽函数对应每个按钮被按下后会调用的内容。(槽函数名需与ui转换来的py文件里的一致)
在终端里启动新写的.py文件,即可打开界面,点击相应按钮,实现相应的功能。效果如下(做的比较丑):
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。