美文网首页
使用UI文件

使用UI文件

作者: 技术喵 | 来源:发表于2019-07-24 17:02 被阅读0次

1.创建UI文件 mainwindow.ui,文件内容如下


1563957535129.png
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>80</y>
      <width>201</width>
      <height>81</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

2.在命令行把ui文件转换为py文件

pyside2-uic mainwindow.ui > ui_mainwindow.py

3.注意这个py文件格式是错误的,我们需要用记事本打开,另存为UTF-8格式


1563957415620-fs8.png

4.在main.py里导入这个文件

from ui_mainwindow import *

5.创建MainWindow类,使用Ui_MainWindow作为UI画面

import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from ui_mainwindow import *

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = MainWindow()
    window.show()

    sys.exit(app.exec_())

6.运行截图


1563958969532.png

相关文章

  • PyCharm+Qt Designer+PyUIC安装配置教程

    使用Qt Designer来设计界面,生成ui文件 使用PyUIC将ui文件转成py文件 Qt Designer和...

  • 使用UI文件

    1.创建UI文件 mainwindow.ui,文件内容如下 2.在命令行把ui文件转换为py文件 3.注意这个py...

  • UI界面控制Demo

    控制UI界面的几种方法 使用XML布局文件控制UI界面 在java代码中控制UI界面 使用XML和Java代码混合...

  • JQUERY UI使用初体验

    JQUERY UI使用初体验:JQUERY UI网址:https://jqueryui.com/ 相关文件下载地址...

  • schedule

    Kendo UI 如何开始使用Kendo UI?准备文件css > bootstrap.min.css ke...

  • UI界面设计方式

    UI界面设计方式 UI制作上逐渐分化为了三种: 使用代码手写UI及布局; 使用单个xib文件组织viewContr...

  • vue+express+mongodb项目(文件目录的创建)

    项目目录: 使用vue ui创建(vue ui创建步骤)。 (2) 创建student-draaw目录文件:vue...

  • flutter lottie 使用的坑

    使用lottie时,UI给的动画资源文件如图 选中这两个文件,右键压缩出来的zip文件才能被lottie使用,如果...

  • android studio 技巧

    AAR 使用 AAR 文件: 专门用于打包UI组件库。与jar相比其多了一些UI组件用到的属性、图片等一系列文件,...

  • 使用PyQt5与QtDesigner配合搭建界面

    之前网上找到的方法大多是将QtDesigner生成的ui文件转成py再使用,这样还是比较麻烦,能不能直接使用ui文...

网友评论

      本文标题:使用UI文件

      本文链接:https://www.haomeiwen.com/subject/rrffrctx.html