【PYQT】使用PYQT Integration对.ui文件转换并实现python pyqt编程

10 篇文章 1 订阅

环境配置

在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__()的区别–博客园

Python super() 函数–菜鸟教程

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()
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
PyQt is a set of Python bindings for the Qt application framework and libraries. It allows you to develop cross-platform desktop applications using Python. Here are the steps to integrate PyQt into your Python project: 1. Install PyQt: You can install PyQt using pip by running the following command in your terminal: `pip install PyQt5` 2. Import PyQt modules: In order to use PyQt in your Python project, you need to import its modules. The most commonly used PyQt modules are `QtCore`, `QtGui`, and `QtWidgets`. Here's an example of how to import them: ```python from PyQt5 import QtCore, QtGui, QtWidgets ``` 3. Create your application window: You can create a window for your application using the `QtWidgets.QMainWindow` class. Here's an example: ```python class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("My App") self.setGeometry(100, 100, 800, 600) self.show() ``` 4. Add widgets to your window: You can add widgets like buttons, labels, and text boxes to your window using the `QtWidgets` module. Here's an example: ```python class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("My App") self.setGeometry(100, 100, 800, 600) # Add a button widget self.button = QtWidgets.QPushButton("Click me", self) self.button.setGeometry(QtCore.QRect(200, 200, 100, 50)) self.show() ``` 5. Run your application: You can run your PyQt application by calling the `QtWidgets.QApplication` class and passing your `MainWindow` object to it. Here's an example: ```python if __name__ == "__main__": app = QtWidgets.QApplication([]) window = MainWindow() app.exec_() ``` This will create an instance of your `MainWindow` class and run the application. You can then interact with the widgets in your window.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值