【PySide2學(xué)習(xí)筆記】1_簡單的QtWidgets應(yīng)用和QtQuick/QML應(yīng)用

1. QtWidgets應(yīng)用

import sys
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
# A QLabel is a widget that can present text (simple or rich, like html), and images
label = QLabel("<font color=red size=40>Hello World!</font>")
# show the label
label.show()
# enter the Qt main loop and start to execute the Qt code
# In reality, it is only here where the label is shown
app.exec_()

運(yùn)行結(jié)果如下:


QtWidgets.png

2. QtQuick/QML應(yīng)用

PySide2/QML應(yīng)用程序至少包含兩個(gè)不同的文件——一個(gè)帶有用戶界面QML描述的文件,以及一個(gè)加載QML文件的Python文件。

  • view.qml

    import QtQuick 2.0
    
    Rectangle {
        width: 200
        height: 200
        color: "green"
    
        Text {
            text: "Hello World"
            anchors.centerIn: parent
        }
    }
    
  • main.py

    from PySide2.QtWidgets import QApplication
    from PySide2.QtQuick import QQuickView
    from PySide2.QtCore import QUrl
    
    # A PySide2/QML application consists, at least, 
    # of two different files - a file with the QML description of the user interface, 
    # and a python file that loads the QML file. 
    
    app = QApplication([])
    view = QQuickView()
    # import QtQuick and set the source of the QQuickView object to the URL of your QML file
    url = QUrl("./view.qml")
    
    # import QtQuick and set the source of the QQuickView object to the URL of your QML file
    # If you are programming for desktop, you should consider 
    # adding view.setResizeMode(QQuickView.SizeRootObjectToView) before showing the view.
    view.setSource(url)
    # view.setResizeMode(QQuickView.SizeRootObjectToView)
    view.show()
    app.exec_()
    
  • 運(yùn)行結(jié)果:


    QML.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容