Python運(yùn)行第一個(gè)PySide2的窗體程序
上一章節(jié)介紹了PySide2的安裝以及如何去啟動(dòng)程序進(jìn)行頁(yè)面設(shè)計(jì),并且將工具集成到pycharm的擴(kuò)展工具中去,有2個(gè)地方寫(xiě)的不對(duì),用的是pyuic工具,需要改一下,改成pyside2-uic.exe。具體改動(dòng)點(diǎn):

pycharm擴(kuò)展工具中的配置也需要調(diào)整一下:

上一篇的配置寫(xiě)的是pyqt5的配置,這里主要采用PySide2進(jìn)行學(xué)習(xí)。
修改為正確的配置后,鼠標(biāo)選中ui文件,右鍵選擇擴(kuò)展工具中的pyside2-uic就可以轉(zhuǎn)換為python腳本。
先看一下我畫(huà)的一個(gè)簡(jiǎn)單的GUI頁(yè)面:

保存頁(yè)面文件后,后綴是.ui的格式,用文本文件打開(kāi)的話,內(nèi)容是xml格式的:

postman.ui源碼:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>948</width>
<height>617</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QComboBox" name="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="lineEdit">
<property name="geometry">
<rect>
<x>170</x>
<y>30</y>
<width>541</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>740</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="textEdit">
<property name="geometry">
<rect>
<x>70</x>
<y>150</y>
<width>821</width>
<height>331</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
轉(zhuǎn)換之后的python腳本:postman.py
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'postman.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class Ui_Dialog(object):
def setupUi(self, Dialog):
if not Dialog.objectName():
Dialog.setObjectName(u"Dialog")
Dialog.resize(948, 617)
self.comboBox = QComboBox(Dialog)
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.setObjectName(u"comboBox")
self.comboBox.setGeometry(QRect(70, 30, 81, 31))
self.lineEdit = QLineEdit(Dialog)
self.lineEdit.setObjectName(u"lineEdit")
self.lineEdit.setGeometry(QRect(170, 30, 541, 31))
self.pushButton = QPushButton(Dialog)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(740, 30, 151, 31))
self.label = QLabel(Dialog)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(70, 90, 72, 15))
self.label_2 = QLabel(Dialog)
self.label_2.setObjectName(u"label_2")
self.label_2.setGeometry(QRect(160, 90, 121, 21))
self.textEdit = QTextEdit(Dialog)
self.textEdit.setObjectName(u"textEdit")
self.textEdit.setGeometry(QRect(70, 150, 821, 331))
self.retranslateUi(Dialog)
QMetaObject.connectSlotsByName(Dialog)
# setupUi
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
self.comboBox.setItemText(0, QCoreApplication.translate("Dialog", u"GET", None))
self.comboBox.setItemText(1, QCoreApplication.translate("Dialog", u"POST", None))
self.pushButton.setText(QCoreApplication.translate("Dialog", u"Send", None))
self.label.setText(QCoreApplication.translate("Dialog", u"Params", None))
self.label_2.setText(QCoreApplication.translate("Dialog", u"Headers", None))
# retranslateUi
單單有以上兩個(gè)腳本是無(wú)法運(yùn)行的,還需要單獨(dú)再寫(xiě)幾行代碼來(lái)加載頁(yè)面窗口進(jìn)行展示:
run_postman.py:
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from postman import Ui_Dialog
if __name__ == "__main__":
# 創(chuàng)建一個(gè)Application對(duì)象
app = QApplication(sys.argv)
# 創(chuàng)建一個(gè)窗體對(duì)象
MainWindow = QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow)
# 設(shè)置窗口顯示
MainWindow.show()
sys.exit(app.exec_())
運(yùn)行后的效果如下圖所示:

大家感興趣的話,可以根據(jù)自己的喜好去調(diào)整頁(yè)面設(shè)計(jì),實(shí)現(xiàn)自己的測(cè)試小工具。
到此這篇關(guān)于Python運(yùn)行第一個(gè)PySide2的窗體程序的文章就介紹到這了,更多相關(guān)Python運(yùn)行第一個(gè)PySide2的窗體程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python基于paramiko庫(kù)遠(yuǎn)程執(zhí)行 SSH 命令,實(shí)現(xiàn) sftp 下載文件
這篇文章主要介紹了python基于paramiko庫(kù)遠(yuǎn)程執(zhí)行 SSH 命令,實(shí)現(xiàn) sftp 下載文件的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03
flask 實(shí)現(xiàn)token機(jī)制的示例代碼
這篇文章主要介紹了flask 實(shí)現(xiàn)token機(jī)制的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Python編譯為二進(jìn)制so可執(zhí)行文件實(shí)例
今天小編就為大家分享一篇Python編譯為二進(jìn)制so可執(zhí)行文件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
解決pygal.style的LightColorizedStyle參數(shù)問(wèn)題
這篇文章主要介紹了解決pygal.style的LightColorizedStyle參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
python人工智能tensorflow函數(shù)tf.nn.dropout使用方法
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.nn.dropout使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python?中的裝飾器實(shí)現(xiàn)函數(shù)的緩存(場(chǎng)景分析)
Python中的裝飾器可以用于實(shí)現(xiàn)函數(shù)的緩存,其原理是在函數(shù)執(zhí)行前,首先判斷傳入的參數(shù)是否在緩存中已經(jīng)存在對(duì)應(yīng)的計(jì)算結(jié)果,這篇文章主要介紹了Python?中的裝飾器可以用于實(shí)現(xiàn)函數(shù)的緩存,需要的朋友可以參考下2023-02-02
Python實(shí)現(xiàn)RLE格式與PNG格式互轉(zhuǎn)
在機(jī)器視覺(jué)領(lǐng)域的深度學(xué)習(xí)中,很多數(shù)據(jù)集的標(biāo)注文件使用RLE的格式。但是神經(jīng)網(wǎng)絡(luò)的輸入一定是一張圖片,為此必須把RLE格式的文件轉(zhuǎn)變?yōu)閳D像格式。本文將利用Python實(shí)現(xiàn)RLE格式與PNG格式互轉(zhuǎn),感興趣的可以了解一下2022-08-08

