【PYQT】使用PYQT Integration对.ui文件转换并实现python pyqt编程
环境配置
在VSCODE拓展中搜索QT,并安装PYQT Integration
。
PYQT Integration的介绍:
**一个扩展帮助你在 vsocde 中编码 PYQT 表单。**支持.ui
、.qrc
、.pro
、.ts
文件。
在VSCODE设置中查找Qtdesigner Path
,即designer.exe的绝对路径。
比如博主的
D:\coding_programs\Qt5.14.2\Tools\QtCreator\bin\qtcreator.exe
设计.ui文件
配置好之后,我们就可以先打开Qt Creator进行一个.ui
文件的设计了。
比如博主设计的login.ui
:
在里面设计好.ui文件后,就可以将该文件放入我们的项目路径下:
.ui文件代码
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>login</class>
<widget class="QDialog" name="login">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>350</width>
<height>350</height>
</rect>
</property>
<property name="windowTitle">
<string>学生学籍信息管理系统</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>login_ui.png</normaloff>login_ui.png</iconset>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>95</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>38</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>密码:</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QLineEdit" name="lineEdit_2"/>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>教师登录</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>用户名:</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>学生登录</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QLineEdit" name="lineEdit"/>
</item>
</layout>
</item>
<item row="1" column="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>48</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>135</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
转化.ui为.py
添加到我们的vscode项目文件夹下,并对其右键:
单击compile form
即可转换.ui
文件为.py
文件。
.py文件的代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'd:\coding_programs\PythonPrograms\student_status_information\login.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_login(object):
def setupUi(self, login):
login.setObjectName("login")
login.resize(350, 350)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("d:\\coding_programs\\PythonPrograms\\student_status_information\\login_ui.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
login.setWindowIcon(icon)
self.gridLayout_2 = QtWidgets.QGridLayout(login)
self.gridLayout_2.setObjectName("gridLayout_2")
spacerItem = QtWidgets.QSpacerItem(20, 95, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout_2.addItem(spacerItem, 0, 1, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(38, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout_2.addItem(spacerItem1, 1, 0, 1, 1)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.label_2 = QtWidgets.QLabel(login)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.lineEdit_2 = QtWidgets.QLineEdit(login)
self.lineEdit_2.setObjectName("lineEdit_2")
self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 2)
self.pushButton_2 = QtWidgets.QPushButton(login)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 2, 2, 1, 1)
self.label = QtWidgets.QLabel(login)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(login)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 2)
self.lineEdit = QtWidgets.QLineEdit(login)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 2)
self.gridLayout_2.addLayout(self.gridLayout, 1, 1, 1, 1)
spacerItem2 = QtWidgets.QSpacerItem(48, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout_2.addItem(spacerItem2, 1, 2, 1, 1)
spacerItem3 = QtWidgets.QSpacerItem(20, 135, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout_2.addItem(spacerItem3, 2, 1, 1, 1)
self.retranslateUi(login)
QtCore.QMetaObject.connectSlotsByName(login)
def retranslateUi(self, login):
_translate = QtCore.QCoreApplication.translate
login.setWindowTitle(_translate("login", "学生学籍信息管理系统"))
self.label_2.setText(_translate("login", "密码:"))
self.pushButton_2.setText(_translate("login", "教师登录"))
self.label.setText(_translate("login", "用户名:"))
self.pushButton.setText(_translate("login", "学生登录"))
Super()和__init__()
python类中super()和__init__()的区别–博客园
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
如上是官网上的标准代码,对其理解
super() 函数是用于调用父类(超类)(此处是
QtWidgets.QWidget
)的一个方法。
super() 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。
super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类 FooChild 的对象转换为类 FooParent 的对象
super(SongBird,self).__init__()
它会查找所有的超类,以及超类的超类,直到找到所需的特性为止。
_init_(self)
函数:单继承时super()
和__init__()
实现的功能是类似的
窗口生成显示代码
弹出窗口设计
先是一个login.ui文件,然后单击登录后,连接数据库查询,并跳转到window.ui。这是一个主窗口,设计了诸多按钮,单击每个窗口即可跳转出对应的子页面并表现出其功能。
login.py
下的代码:
self.xxxbutton.clicked.connect(self.window)
def window(self):
self.window=UI_FORM()
self.window.show()