1. QtWidgets应用
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_()
运行结果如下:
QtWidgets.png
2. QtQuick/QML应用
PySide2/QML应用程序至少包含两个不同的文件——一个带有用户界面QML描述的文件,以及一个加载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_()
-
运行结果:
QML.png
网友评论