PYQT5 界面分离

在QT designer 中完成自己的布局后,我们点击保存按钮。
文件的命名是以.ui保存。保存后的文件是以xml描述的布局信息。

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>640</width>
    <height>480</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QLineEdit" name="lineEdit">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>90</y>
     <width>113</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButto1">
   <property name="geometry">
    <rect>
     <x>60</x>
     <y>150</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="cursor">
    <cursorShape>CrossCursor</cursorShape>
   </property>
   <property name="text">
    <string>PushButton</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

显然这样的文件我们无法使用。例如我这里需要对一个按钮的名称进行修改,然后在特定的时候把按钮隐藏起来。我们会发,按钮的实体在哪呢?代码呢?我们放上去的各种控件,我们在代码上如何对他操作呢?pyqt提供了一套工具,可以把.ui文件转换为.py文件

在这里插入图片描述
上图的PyUIC工具实现了.UI–>.py文件的转换

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.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_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(640, 480)
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(50, 90, 113, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.pushButto1 = QtWidgets.QPushButton(Form)
        self.pushButto1.setGeometry(QtCore.QRect(60, 150, 93, 28))
        self.pushButto1.setCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
        self.pushButto1.setObjectName("pushButto1")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButto1.setText(_translate("Form", "PushButton"))

很好,在.py文件中可以看到窗口定义了各种控件,使用了各种控件的方法。我们使用工具把.UI转换为.py文件,这两个文件一一对应。简单说就是在QT designer 中完成自己的布局后通过转换生成一个.py文件。如果我们手动在生成的py文件中修改内容,是可以运行的。但是当再次用QT designer修改布局后,去生成.py 那么手动写入的代码就会被覆盖,就是白写了。所以生成的这个文件我们不会把自己的代码写入其中。所以这个文件代表的是界面,是QT designer工具所生成的界面。如果项目中关于界面的部分比较简单仅仅靠QT designer下的设计就能完成的话,那么界面文件就是我们转换后的.py文件了。如果使用代码再次对界面进行编辑,而我们在文件上又要分离界面文件,这样我们还可以包一层文件。简单说就是我们建立一个文件 去继承自动生成的.py文件。

from PyQt5.QtWidgets import QApplication, QMainWindow
from untitled import *



class MyWindow(QMainWindow,Ui_Form):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.setupUi(self)

这样就完成了,对自动生成的.py文件的窗口继承。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值