1. 最简搭建方案
本来想说,要玩PyQt5,要先懂一些Python3和Qt5的基础知识,但怕这样一说会吓退一批人。好吧,咱们就来个0基础,傻瓜化,跟我来。
Python3环境会搭建吗?Spyder呢?不会的参考这里https://blog.csdn.net/asanscape/article/details/80155852,会的话,没了。这样就可以直接跑PyQt5程序了。不信?复制下段代码,贴到Spyder里试试:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.setWindowModality(QtCore.Qt.WindowModal)
mainWindow.resize(624, 511)
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "hello word"))
if __name__ == "__main__": #I注意,我们是从这里开始运行的!
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
ui = Ui_mainWindow()
ui.setupUi(mainWindow)
mainWindow.resize(624, 511)
mainWindow.setFixedSize(624,511)
mainWindow.setWindowTitle('hello world')
mainWindow.show()
sys.exit(app.exec_())
难道Python3自带PyQt5吗?其实只是一个包而已,因为Spyder就是用PyQt5开发的,所以,我们完全可以做到和它一样的美丽程序--噗!不赞同,没看出来它美啊!那么它很强大,好了吧?
2. 给程序换个图标。它不仅支持ICO,还支持PNG哦!
from PyQt5.QtGui import QIcon
mainWindow.setWindowIcon(QIcon('logo.png'))
嗯,要知道一些Python的规矩,例如,用缩进来代表代码块,比如if, for啥的,就不用{}括起来了,你缩进得不对,运行的结果就可能不对,甚至不能运行。
3. 创建一个标签和按钮,并绑定事件,哦,应该叫绑定信号和槽
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.setWindowModality(QtCore.Qt.WindowModal)
mainWindow.resize(624, 511)
self.centralWidget = QtWidgets.QWidget(mainWindow)
self.centralWidget.setObjectName("centralWidget")
self.label = QtWidgets.QLabel(self.centralWidget)
self.label.setGeometry(QtCore.QRect(60, 60, 191, 61))
self.label.setText("画个标签试试!")
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(240, 240,200, 53))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("点我试试!")
self.pushButton.setFlat(True)
self.pushButton.setStyleSheet("background-color: rgb(244, 1, 5);"
"border-color: rgb(170, 150, 163);"
"font: 75 12pt \"Arial Narrow\";"
"color: rgb(126, 255, 46);")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon)
self.pushButton.setIconSize(QtCore.QSize(50, 80))
self.pushButton.setAutoRepeatDelay(200)
mainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(mainWindow)
self.pushButton.clicked.connect(self.setText_qlabel) #绑定的代码
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle('您好')
mainWindow.setWindowIcon(QIcon('logo.png'))
def setText_qlabel(self):
self.label.setText('改个标签试试!')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
ui = Ui_mainWindow()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())