首頁>技術>

1 說明:

=====

1.1 PySide2 :這個QT的親兒子最近(2018年7月)才出生,而且持續有團隊在維護。

1.2 PyQt5:是在PySide2之前的,Qt收的義子 (Riverbank Computing這個公司開發的)。

1.3 Qt庫裡面有非常強大的圖形介面開發庫,但是Qt庫是C++語言開發的,PySide2、PyQt5可以讓我們通過Python語言使用Qt。推薦使用:PySide2。(終於理順關係了)

1.4 基於Qt 的Python庫

1.4.1 優點是控制元件比較豐富、跨平臺體驗好、文件完善、使用者多。

1.4.2 缺點是 庫比較大,釋出出來的程式比較大;當然是個好東西,總得介紹介紹。

1.5 大家要開發小工具,介面比較簡單,可以採用appJar,EasyGUI,PySimpleGUI,我都有介紹:

2 安裝:

======

2.1 環境:華為膝上型電腦、深度deepin-linux作業系統、python3.8和微軟vscode編輯器。

2.2 安裝:

2.3 官網:

https://doc.qt.io/qtforpython/index.htmlhttps://wiki.qt.io/Qt_for_Pythonhttps://forum.qt.io/category/58/qt-for-pythonhttps://pypi.org/project/PySide2/ #最新版https://wiki.qt.io/PySide2

2.4 竟然沒有github地址:

/file/2020/06/12/20200612022443_2517.jpg #PySide (deprecated),這個地址指已經棄用了

3 helloworld:最簡單的一個GUI視窗開始學習:

====================================

3.1 多種方法的程式碼:

#方法一from PySide2.QtWidgets import QApplication, QLabel  app = QApplication()label = QLabel("Hello World")label.show()app.exec_()
#方法二from PySide2.QtWidgets import QApplication, QLabel   if __name__ == "__main__": app = QApplication() label = QLabel("Hello World") label.show() app.exec_()
#方法三from PySide2.QtWidgets import QApplication, QLabeldef main(): app = QApplication() label = QLabel("Hello World") label.show() app.exec_()   if __name__ == "__main__": main()
#方法四import sysfrom PySide2.QtWidgets import QApplication, QLabel   if __name__ == "__main__": app = QApplication(sys.argv) #python的sys.argv的知識點 label = QLabel("Hello World") label.show() sys.exit(app.exec_())

3.2 python的sys.argv:(引申)

3.2.1 sys.argv[0]表示程式碼本身檔案路徑。

3.2.2 sys.argv[ ]其實就是一個列表,裡邊的項為使用者輸入的引數,關鍵就是要明白這引數是從程式外部輸入的,而非程式碼本身的什麼地方,要想看到它的效果就應該將程式儲存了,從外部來執行程式並給出引數。

3.3 效果圖:

4 美化上述:

=========

4.1 QLabel的文字的字型大小和顏色:

from PySide2.QtWidgets import QApplication, QLabelapp = QApplication()#文字設定:字型大小和顏色定義label = QLabel("<font color=red size=40>Hello World!</font>")label.show()app.exec_()

4.2 視窗大小、位置和標題名:

#方法一from PySide2.QtWidgets import QApplication, QWidgetclass Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("中文顯示視窗") #視窗位置和大小 self.setGeometry(300,300, 500,400) self.setIcon()#myApp = QApplication(sys.argv)app = QApplication() #與上面相同window = Window()window.show()app.exec_()
#方法二:視窗大小、位置和標題名,帶有視窗內容顯示文字的#增加控制元件:QWidget,QFormLayout,QVBoxLayoutfrom PySide2.QtWidgets import QApplication, QLabel,QWidget,QFormLayout,QVBoxLayoutapp = QApplication()#自定義Windowti=window title=視窗相關設定:類(python的基礎知識)class Windowti(QWidget): #初始化特性 def __init__(self): QWidget.__init__(self) #視窗標題名 self.setWindowTitle('你好世界!HelloWorld!') #支援中文 #self.setMinimumWidth(400) #最小視窗寬度 #視窗大小和位置 #x和y是座標,左上角座標為0,0 #w和h是寬和高,即視窗大小 #setGeometry(x: int, y: int, w: int, h: int) self.setGeometry(10,10,500,500) # Create the QVBoxLayout that lays out the whole form self.layout = QVBoxLayout() # Create the form layout that manages the labeled controls self.form_layout = QFormLayout() self.greeting = QLabel('', self) #self.form_layout.addRow('Hello World!', self.greeting) self.form_layout.addRow('<font color=red size=40>Hello World!</font>', self.greeting) # Add the form layout to the main VBox layout self.layout.addLayout(self.form_layout) # Set the VBox layout as the window's main layout self.setLayout(self.layout) def run(self): # Show the form self.show() # Run the qt application app.exec_()myfirstapp=Windowti()myfirstapp.run()

效果圖:

4.3 qml法:

4.3.1 資料夾:1hw下面有2個檔案:main.py和mian.qml

4.3.2 main.py程式碼:

from PySide2.QtWidgets import QApplicationfrom PySide2.QtQml import QQmlApplicationEngineif __name__ == "__main__": app = QApplication() #注意main.qml檔案的目錄和路徑 engine = QQmlApplicationEngine("/home/xgj/Desktop/pyside2/1hw/main.qml") app.exec_()

4.3.3 main.qml程式碼:注意:安裝外掛:Qt for python,這樣qml程式碼才有高亮。

import QtQuick 2.7import QtQuick.Window 2.3import QtQuick.Controls 2.3ApplicationWindow { id: _window // 視窗標題設定 title: "qml顯示視窗Helloworld!" // 視窗大小和位置的設定 width: 800 height: 500 x:300 y:300 // Window預設不可見,需要進行設定為可見 visible: true}

4.3.4 執行效果圖:

5 圖片顯示:

========

5.1 靜態圖片顯示:

from PySide2.QtWidgets import QApplication, QLabelfrom PySide2.QtGui import QPixmap#app = QApplication([])app = QApplication() #等同於上面Label = QLabel()#指定圖片的目錄和路徑,圖片格式:jpeg,jpg,png,ico均可PixMap = QPixmap("/home/xgj/Desktop/pyside2/20.jpeg")#PixMap = QPixmap("/home/xgj/Desktop/pyside2/18.jpg")#PixMap = QPixmap("/home/xgj/Desktop/pyside2/1231.png")#PixMap = QPixmap("/home/xgj/Desktop/pyside2/1.ico")#PixMap = QPixmap("/home/xgj/Desktop/pyside2/yytd.gif") #顯示靜態的,不是gif動態的Label.setPixmap(PixMap)Label.show()app.exec_()

效果圖:

5.2 gif圖顯示:

from PySide2.QtWidgets import QApplication, QLabelfrom PySide2.QtGui import QMovie#app = QApplication([])app = QApplication()Label = QLabel()#注意不要把這行放到例項化app物件的上面,會無效#gif因為是動態的,屬於movie動畫類Movie = QMovie("/home/xgj/Desktop/pyside2/yytd.gif") #可以#Movie = QMovie("/home/xgj/Desktop/pyside2/test.mp4") #報錯Label.setMovie(Movie)#這句表示播放動畫,不能省略。也不能放到show後面,否則無法自適應大小Movie.start()  Label.show()app.exec_()

效果圖:

====很仔細,初始pyside2===

259

Qt

Python

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 記一次Docker中Redis連線暴增的問題排查