- 首先创建一个主窗口,命名为Login.py
注:这一步可以通过QTDesigner工具来画出图形,然后生成.py文件,再做修改
如何生成.py文件的过程已在上一篇博客发布,谢谢关注
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
#定义一个建立UI界面的函数
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow") #设置窗口名称
MainWindow.resize(386, 127) #设置窗口大小
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) #设置主窗口名称
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_())
运行的结果:
- 可以美化界面,添加logo和背景图
from PyQt5.QtGui import QIcon
MainWindow.setWindowIcon(QIcon('logo.png'))
MainWindow.setStyleSheet("background-image:url(Background.jpg)")
- 到这一步的所有代码
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(386, 127)
MainWindow.setWindowIcon(QIcon('logo.png'))
MainWindow.setStyleSheet("background-image:url(Background.jpg)")
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "一颗数据小白菜"))
if __name__ ==<