前言:
今天小伙伴们对“python文件间传递参数”大概比较注意,小伙伴们都想要了解一些“python文件间传递参数”的相关资讯。那么小编在网络上收集了一些对于“python文件间传递参数””的相关文章,希望你们能喜欢,咱们快快来了解一下吧!本示例python文件main.py代码与注释如下:
import osfrom pathlib import Pathimport sysfrom PySide6.QtCore import QObject, QTimer, QUrl, Signal, Slotfrom PySide6.QtGui import QGuiApplicationfrom PySide6.QtQuick import QQuickViewfrom PySide6.QtQml import QmlElementQML_IMPORT_NAME = "examples.signals.pytoqml2"#定义包名称QML_IMPORT_MAJOR_VERSION = 1#定义包版本@QmlElement#装饰器把类暴露给qmlclass RotateValue(QObject): valueChanged = Signal(int, arguments=['val']) def __init__(self): super().__init__() self.r = 0 @Slot() def increment(self): self.r = self.r + 10 self.valueChanged.emit(self.r)if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() rotatevalue = RotateValue()#定义 timer = QTimer()#设置定时器 timer.start(500)#定时0.5秒开始 view.setInitialProperties({"rotatevalue": rotatevalue})#设置rotatevalue qml_file = os.fspath(Path(__file__).resolve().parent / 'view.qml') view.setSource(QUrl.fromLocalFile(qml_file)) if view.status() == QQuickView.Error: sys.exit(-1) timer.timeout.connect(rotatevalue.increment)#连接信号 view.show() res = app.exec() del view sys.exit(res)
本示例qml文件view.qml界面代码与注释如下:
import QtQuickimport QtQml//导入定义的包import examples.signals.pytoqml2 1.0Rectangle { id: page width: 500; height: 200 color: "lightgray" required property RotateValue rotatevalue//定义RotateValue属性 Text { id: helloText text: "Hello world!" anchors.horizontalCenter: page.horizontalCenter y: 30 font.pointSize: 24; font.bold: true } Connections {//连接信号 target: rotatevalue function onValueChanged(val) { helloText.rotation = val//改变helloText的位置 } }}
标签: #python文件间传递参数