Python GUI编程:PySide2页面设计优化

简介: 通过之前的文章,我们发现:在拖拽控件的时候,页面每一个控件的名称没有跳转,都是用的默认的,这样不方便后期去按钮,输入框等进行其他相关操作,会导致代码可读性差,接下来可以进行优化。

前置条件:

Python GUI编程:PySide2介绍

Python GUI编程:如何运行第一个PySide2的窗体程序

通过之前的文章,我们发现:在拖拽控件的时候,页面每一个控件的名称没有跳转,都是用的默认的,这样不方便后期去按钮,输入框等进行其他相关操作,会导致代码可读性差,接下来可以进行优化:

1、针对拖拽生成的控件修改名称

以下图为例,在Qt Designer软件中,点击对应的控件,可以看到控件的命名:


微信图片_20220114151127.png


下拉框默认是comboBox,页面有多个同类控件的话,默认会加下标区分。这里可以将控件命名改一下,方便后续使用。


微信图片_20220114151130.png


比如,上面的控件表示请求方式,就可以改为method_comboBox,修改之后的ui文件的文本内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>PostmanTools</class>
 <widget class="QDialog" name="PostmanTools">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>948</width>
    <height>617</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>简易Postman Tools</string>
  </property>
  <widget class="QComboBox" name="method_comboBox">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>30</y>
     <width>81</width>
     <height>31</height>
    </rect>
   </property>
   <item>
    <property name="text">
     <string>GET</string>
    </property>
   </item>
   <item>
    <property name="text">
     <string>POST</string>
    </property>
   </item>
  </widget>
  <widget class="QLineEdit" name="url_text">
   <property name="geometry">
    <rect>
     <x>170</x>
     <y>30</y>
     <width>541</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="send_btn">
   <property name="geometry">
    <rect>
     <x>760</x>
     <y>30</y>
     <width>151</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>Send</string>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>90</y>
     <width>72</width>
     <height>15</height>
    </rect>
   </property>
   <property name="text">
    <string>Params</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>160</x>
     <y>90</y>
     <width>121</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>Headers</string>
   </property>
  </widget>
  <widget class="QTextEdit" name="res_textEdit">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>150</y>
     <width>821</width>
     <height>331</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="exit_btn">
   <property name="geometry">
    <rect>
     <x>280</x>
     <y>90</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>Exit</string>
   </property>
  </widget>
  <widget class="QPushButton" name="reset_btn">
   <property name="geometry">
    <rect>
     <x>420</x>
     <y>90</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>重置</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

2、可以直接在GUI设计页面对按钮做一些简单的事件处理:


微信图片_20220114151133.png

相关文章
|
4月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
333 102
|
4月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
356 104
|
4月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
285 103
|
3月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
271 3
|
3月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
509 3
|
3月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
333 3
|
3月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
356 0
|
4月前
|
数据采集 网络协议 API
协程+连接池:高并发Python爬虫的底层优化逻辑
协程+连接池:高并发Python爬虫的底层优化逻辑
|
4月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
210 82
|
4月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的多面手
Python:现代编程的多面手
102 0

推荐镜像

更多