首頁 > 軟體

python之PySide2安裝使用及QT Designer UI設計案例教學

2021-07-26 16:01:08

pyside2 

>>> pip install pyside2

 QT Designer

>>> pip show pyside2
 
Location: c:usersAdministratorappdataroamingpythonpython39site-packages
 
 
檔案位置
C:UsersAdministratorAppDataRoamingPythonPython39site-packagesPySide2designer.exe

Unable to lanch C: 

PySide2目錄下複製uic.exe到新建的bin資料夾

C:...PythonPython39site-packagesPySide2binuic.exe

Ⅰ. Init 

from PySide2.QtWidgets import *
 
class Window:
    def __init__(self):
        self.win = QMainWindow()
 
if __name__ == '__main__':
    app = QApplication([])
    w = Window()
    w.win.show()
    app.exec_()
from PySide2.QtWidgets import *
 
if __name__ == '__main__':
    app = QApplication([])
    win = QMainWindow()
    win.show()
    app.exec_()

Ⅱ Example

from PySide2.QtWidgets import *
 
 
class Window:
    def __init__(self):
        self.win = QMainWindow()
        self.win.resize(500, 400)
        self.win.move(700, 210)
        self.win.setWindowTitle("測試視窗")
 
 
        self.textEdit = QTextEdit(self.win)
        self.textEdit.move(50, 50)
        self.textEdit.resize(200, 300)
        self.textEdit.setPlaceholderText("請輸入文字")
 
 
        self.button = QPushButton("button", self.win)
        self.button.move(300, 100)
        self.button.clicked.connect(self.btnClick)
 
 
    def btnClick(self):
        info = self.textEdit.toPlainText()   # 獲取文字資訊
        print(info)
        # print("button被點選")
 
 
if __name__ == '__main__':
    app = QApplication([])
    # win = QMainWindow()
    # win.show()
    w = Window()
    w.win.show()
    app.exec_()

QT Designer

1.自動生成程式碼

...
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
 
    ...
 
if __name__ == "__main__":
    app = QApplication([])
    win = QMainWindow()
    w = Ui_Form()
    w.setupUi(win)
    win.show()
    app.exec_()

2.動態載入ui檔案

from PySide2.QtWidgets import *
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
 
 
class Window:
    def __init__(self):
        super(Window, self).__init__()
 
        # 從檔案中載入UI定義
        qfile = QFile("demo1.ui")
        qfile.open(QFile.ReadOnly)
        qfile.close()
 
        # 從UI定義中動態建立一個相應的視窗物件, 將其內部檔案定義為self.ui.button
        self.ui = QUiLoader().load(qfile)
 
        self.ui.button.clicked.connect(self.btnClick)
        # self.button.clicked.connect(self.btnClick)
 
    def btnClick(self):
        info = self.ui.textEdit.toPlainText()   # 獲取文字資訊
        # info = self.textEdit.toPlainText()
        print(info)
 
if __name__ == '__main__':
    app = QApplication([])
    w = Window()
    w.ui.show()
    # w.win.show()
    app.exec_()

到此這篇關於python之PySide2安裝使用及QT Designer UI設計案例教學的文章就介紹到這了,更多相關python之PySide2安裝使用及QT Designer UI設計內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com