PyQt5入门
要使用pyqt5需要先导入对应的包
pip install PyQt5
pip install PyQt5-tools
然后编写我们的第一个程序
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt初体验")
self.resize(600,700)
self.setip_ui()
def setip_ui(self):
label=QLabel(self)
label.resize(100,100)
label.setText("欢迎使用PyQt")
label.move(250,200)
if __name__ == '__main__':
app=QApplication(sys.argv)
window=MyWindow()
window.show()
sys.exit(app.exec_())
接下来用pyqt5写了一个简单的加法计算器
from PyQt5 import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from pyqt5.UI.Add import *
import sys
class MainWindow(Ui_AddTool,QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.retranslateUi(self)
if __name__ == '__main__':
app=QApplication(sys.argv)
window=MainWindow()
window.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_AddTool(object):
def setupUi(self, AddTool):
AddTool.setObjectName("AddTool")
AddTool.resize(1297, 838)
self.centralwidget = QtWidgets.QWidget(AddTool)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setObjectName("groupBox")
self.gridLayout = QtWidgets.QGridLayout(self.groupBox)
self.gridLayout.setObjectName("gridLayout")
spacerItem = QtWidgets.QSpacerItem(236, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 0, 1, 1, 1)
self.Rnum = QtWidgets.QLineEdit(self.groupBox)
self.Rnum.setMinimumSize(QtCore.QSize(50, 20))
self.Rnum.setMaximumSize(QtCore.QSize(300, 50))
self.Rnum.setObjectName("Rnum")
self.gridLayout.addWidget(self.Rnum, 0, 4, 1, 1)
self.label_3 = QtWidgets.QLabel(self.groupBox)
self.label_3.setMinimumSize(QtCore.QSize(80, 50))
self.label_3.setMaximumSize(QtCore.QSize(100, 80))
font = QtGui.QFont()
font.setPointSize(19)
self.label_3.setFont(font)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 0, 5, 1, 1)
self.label_2 = QtWidgets.QLabel(self.groupBox)
self.label_2.setMinimumSize(QtCore.QSize(80, 50))
self.label_2.setMaximumSize(QtCore.QSize(50, 80))
font = QtGui.QFont()
font.setPointSize(20)
font.setStyleStrategy(QtGui.QFont.PreferDefault)
self.label_2.setFont(font)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 0, 2, 1, 1)
self.Lnum = QtWidgets.QLineEdit(self.groupBox)
self.Lnum.setMinimumSize(QtCore.QSize(50, 20))
self.Lnum.setMaximumSize(QtCore.QSize(300, 50))
self.Lnum.setObjectName("Lnum")
self.gridLayout.addWidget(self.Lnum, 0, 0, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(236, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem1, 0, 3, 1, 1)
self.res = QtWidgets.QLabel(self.groupBox)
self.res.setMinimumSize(QtCore.QSize(80, 50))
self.res.setMaximumSize(QtCore.QSize(100, 80))
self.res.setAlignment(QtCore.Qt.AlignCenter)
self.res.setObjectName("res")
self.gridLayout.addWidget(self.res, 0, 6, 1, 1)
spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem2, 4, 2, 1, 1)
self.getres = QtWidgets.QPushButton(self.groupBox)
self.getres.setObjectName("getres")
self.getres.clicked.connect(self.getaddres)
self.gridLayout.addWidget(self.getres, 5, 2, 1, 1)
self.line = QtWidgets.QFrame(self.groupBox)
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.gridLayout.addWidget(self.line, 1, 0, 2, 7)
self.gridLayout_2.addWidget(self.groupBox, 0, 0, 1, 1)
AddTool.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(AddTool)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1297, 26))
self.menubar.setObjectName("menubar")
AddTool.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(AddTool)
self.statusbar.setObjectName("statusbar")
AddTool.setStatusBar(self.statusbar)
self.retranslateUi(AddTool)
QtCore.QMetaObject.connectSlotsByName(AddTool)
def getaddres(self):
self.res.setText(str(int(self.Lnum.text())+int(self.Rnum.text())))
def retranslateUi(self, AddTool):
_translate = QtCore.QCoreApplication.translate
AddTool.setWindowTitle(_translate("AddTool", "MainWindow"))
self.groupBox.setTitle(_translate("AddTool", "加法计算器"))
self.label_3.setText(_translate("AddTool", "="))
self.label_2.setText(_translate("AddTool", "+"))
self.res.setText(_translate("AddTool", "答案"))
self.getres.setText(_translate("AddTool", "计算"))