使用python制作窗体应用

news/2024/7/24 2:29:13 标签: python, qt, pyqt

python_0">使用python制作窗体应用【二】

  1. 打开Qtdesigner,该软件主要用于制作UI界面,如果使用过C#语言进行窗体应用开发的同学肯定会知道,C#的窗体界面无需代码,仅需拖拉拽即可,即所谓的所见即所得,Qtdesigner也是如此。Qt的使用,在此不再赘述。
    在这里插入图片描述
  2. 使用Qt制作如下界面,此处可自行发挥,制作完毕后,保存文件,文件后缀名为.ui,代码如下,读者可复制到.txt文本文件中,然后将以下内容粘贴进去,保存然后将文件名称改为aa.ui。
<?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>709</width>
    <height>560</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>80</x>
      <y>440</y>
      <width>151</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>确定</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>470</x>
      <y>440</y>
      <width>151</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>取消</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>360</y>
      <width>105</width>
      <height>22</height>
     </rect>
    </property>
    <property name="text">
     <string>大猪蹄子</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox_2">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>110</y>
      <width>191</width>
      <height>51</height>
     </rect>
    </property>
    <property name="text">
     <string>渣男</string>
    </property>
    <property name="autoRepeatDelay">
     <number>400</number>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox_3">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>210</y>
      <width>105</width>
      <height>22</height>
     </rect>
    </property>
    <property name="text">
     <string>大渣男</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox_4">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>290</y>
      <width>105</width>
      <height>22</height>
     </rect>
    </property>
    <property name="text">
     <string>大大渣男</string>
    </property>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>240</x>
      <y>50</y>
      <width>321</width>
      <height>18</height>
     </rect>
    </property>
    <property name="text">
     <string>请问你的男朋友是?</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>709</width>
     <height>30</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

在这里插入图片描述
3. 检查python目录下的【script】目录下,是否存在“pyuic5.exe”文件,如存在,找到上一步保存的.ui文件所在的目录,按住shift,鼠标右键单击,选择【在此处打开PowerShell窗口】,打开控制台。
在这里插入图片描述
5. 在控制台输入“pyuic5 -o destination.py aa.ui”。‘-o’代表需生成一个文件,destination.py代表生成.py文件的名称,‘aa.ui’此处为上一步笔者生成的ui文件,如图为生成的文件。
注意】如果存在该exe文件,但是在控制台中输入提示不存在该命令,则鼠标右键单击【我的电脑】|【属性】|【高级系统设置】|【环境变量】|【系统变量】|【Path】,将python/script(pyuic5.exe所在目录)目录添加到其中即可。
在这里插入图片描述生成的destination.py源码如下:

python"># -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'aa.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(709, 560)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(80, 440, 151, 51))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(470, 440, 151, 51))
        self.pushButton_2.setObjectName("pushButton_2")
        self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(310, 360, 105, 22))
        self.checkBox.setObjectName("checkBox")
        self.checkBox_2 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_2.setGeometry(QtCore.QRect(310, 110, 191, 51))
        self.checkBox_2.setAutoRepeatDelay(400)
        self.checkBox_2.setObjectName("checkBox_2")
        self.checkBox_3 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_3.setGeometry(QtCore.QRect(310, 210, 105, 22))
        self.checkBox_3.setObjectName("checkBox_3")
        self.checkBox_4 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_4.setGeometry(QtCore.QRect(310, 290, 105, 22))
        self.checkBox_4.setObjectName("checkBox_4")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(240, 50, 321, 18))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 709, 30))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "确定"))
        self.pushButton_2.setText(_translate("MainWindow", "取消"))
        self.checkBox.setText(_translate("MainWindow", "大猪蹄子"))
        self.checkBox_2.setText(_translate("MainWindow", "渣男"))
        self.checkBox_3.setText(_translate("MainWindow", "大渣男"))
        self.checkBox_4.setText(_translate("MainWindow", "大大渣男"))
        self.label.setText(_translate("MainWindow", "请问你的男朋友是?"))


  1. 在pycham中打开该.py文件,运行后发现并无窗体出现。只需要在末尾添加一个主程序入口,代码如下【一般将主程序入口与函数分离,不放在同一文件中,此处为示例】,运行后出现窗体,该窗体即为在qt中设计的窗体。
python">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_())

在这里插入图片描述【完整代码如下】***

python"># -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'aa.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(709, 560)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(80, 440, 151, 51))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(470, 440, 151, 51))
        self.pushButton_2.setObjectName("pushButton_2")
        self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(310, 360, 105, 22))
        self.checkBox.setObjectName("checkBox")
        self.checkBox_2 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_2.setGeometry(QtCore.QRect(310, 110, 191, 51))
        self.checkBox_2.setAutoRepeatDelay(400)
        self.checkBox_2.setObjectName("checkBox_2")
        self.checkBox_3 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_3.setGeometry(QtCore.QRect(310, 210, 105, 22))
        self.checkBox_3.setObjectName("checkBox_3")
        self.checkBox_4 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_4.setGeometry(QtCore.QRect(310, 290, 105, 22))
        self.checkBox_4.setObjectName("checkBox_4")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(240, 50, 321, 18))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 709, 30))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "确定"))
        self.pushButton_2.setText(_translate("MainWindow", "取消"))
        self.checkBox.setText(_translate("MainWindow", "大猪蹄子"))
        self.checkBox_2.setText(_translate("MainWindow", "渣男"))
        self.checkBox_3.setText(_translate("MainWindow", "大渣男"))
        self.checkBox_4.setText(_translate("MainWindow", "大大渣男"))
        self.label.setText(_translate("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_())

【末尾总结】

PyQt可使用一行代码便可将Qt生成的Ui文件转换为Python代码,弥补了python制作窗体应用的短板,但相较于C#而言,该方面还是有较大缺点。


http://www.niftyadmin.cn/n/1845204.html

相关文章

jquery extend()方法

定义和用法 jQuery.extend() 函数用于将一个或多个对象的内容合并到目标对象。 注意&#xff1a; 如果只为$.extend()指定了一个参数&#xff0c;则意味着参数target被省略。此时&#xff0c;target就是jQuery对象本身。通过这种方式&#xff0c;我们可以为全局对象jQuery添…

Arcscene三维显示异常

Arcscene进行三维显示所有过程均正确操作&#xff0c;但无法正常三维地形显示。原因&#xff1a;未进行投影。解决方法&#xff1a;使用栅格投影工具进行投影。

js 享元模式

享元模式 享元模式&#xff08;Flyweight Pattern&#xff09;主要用于减少创建对象的数量&#xff0c;以减少内存占用和提高性能。这种类型的设计模式属于结构型模式&#xff0c;它提供了减少对象数量从而改善应用所需的对象结构的方式。 享元模式尝试重用现有的同类对象&am…

Arcengine属性选取

以下函数为在Arcengine中通过图层序号以及行列号来进行属性查询。 index代表所需选择要素所在图层&#xff0c;row为该要素行号&#xff0c;column为列号&#xff0c;返回值是string型。如有疑问&#xff0c;请发送邮件至本人。 public string Get_AttByIndexRC(int index, int…

vscode 设置{}

使用vscode的过程中 自己或者push代码以后 代码的格式可能会出现错乱, 作为一个成熟的开发 , 当然应该遵守一些代码规范, 首先代码整洁度应该排在第一位 使用vscode 格式化代码 在少量的情况下 可能可以手动去修改格式, 但在大量格式排版不对齐的情况下, 应该怎么做? 文件 -&…

C#复制数组

不知道你是不有种疑惑&#xff0c;新建一个string[]&#xff0c;随后对其赋值&#xff0c;然后将数组插入到list容器中&#xff0c;重复操作&#xff0c;但是最后发现list中的所有单元均一样&#xff0c;这是因为你始终插入的都是一开始的数组。使用这种方法&#xff0c;可解决…

CSS样式揭秘

修改组件样式: 1.去背景:background:none; 2.去边框下划线:box-shadow: none; 3.去鼠标事件:cursor: default; 3.背景图片居中:background-position: center; 4.背景图片随边框变化:background-size: contain; 4.1.background-size:contain;等比缩放,按照某一边来覆盖…

ArcMap影像地理配准过程配准工具条为灰色

当在ArcMap中进行地理配准时&#xff0c;经常会遇到配准工具条为灰色的情况&#xff0c;此时&#xff0c;一般是由于矢量与栅格或数据框的坐标系不同导致&#xff0c;解决方法如下&#xff1a; 1.右键单击数据框&#xff0c;打开数据框属性&#xff0c;进入数据框坐标系&#x…