最近学习python,用pycharm软件自带的tkinter学习做了UI界面,发现做简单的界面还行,复杂点的就很难受,特别是对于我这种学习没多久又没人带的,就只能各种摸索,然后有一天突然发现了pyqt5这个东西,感觉打开了新大陆一样,然后自己尝试学习网上的教程,自己摸索,做了一个简单的计算器的UI,话不多说,下面教程大致介绍了如何制作,稍微简略,程序部分会比较详细。
如何安装pyqt5和pyqt5tool这里就不赘述了,能够找到详细的教程,也比较简单。
首先新建一个窗口,并且拖动所需要的功能块到窗口,设计出简单的计算器界面。
1、拖动lineEdit作为显示数字界面
2、拖动pushButton作为功能按钮,并且修改名称
3、编辑好的界面保存,并且转换成.py文件
cmd打开终端,输入指令:pyuic5 qwer.ui > calculate.py 或者指令 pyuic5 -o calculate.py qwer.ui
打开转换好的.py文件,得到一个编写好界面的class,代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'qwer.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# 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(499, 559)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
self.lineEdit.setSizePolicy(sizePolicy)
self.lineEdit.setMinimumSize(QtCore.QSize(10, 82))
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
spacerItem = QtWidgets.QSpacerItem(20, 39, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
self.verticalLayout.addItem(spacerItem)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.pushButton_1 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_1.setMinimumSize(QtCore.QSize(60, 60))
self.pushButton_1.setMaximumSize(QtCore.QSize(60, 60))
self.pushButton_1.setObjectName("pushButton_1")
self.gridLayout.addWidget(self.pushButton_1, 0, 0, 1, 1)
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setMinimumSize(QtCore.QSize(60, 60))
self.pushButton_5.setMaximumSize(QtCore.QSize(60, 60))
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout.addWidget(self.pushButton_5, 0, 1, 1, 1)
self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_9.setMinimumSize(QtCore.QSize(60, 60))
self.pushButton_9.setMaximumSize(QtCore.QSize(60, 60))
self.pushButton_9.setObjectName("pushButton_9")
self.gridLayout.addWidget(self.pushButton_9, 0, 2, 1, 1)
self.pushButton_13 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_13.setMinimumSize(QtCore.QSize(60, 60))
self.pushButton_13.setMaximumSize(QtCore.QSize(60, 60))
self.pushButton_13.setObjectName("pushButton_13")
self.gridLayout.addWidget(self.pushButton_13, 0, 3, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setMinimumSize(QtCore.QSize(60, 60))
self.pushButton_2.setMaximumSize(QtCore.QSize(60, 60))
self