pyqt简单介绍和使用

时间:2023-02-10 16:28:04

QML和PyQT5联合编程

安装pyqt

pip3 install PyQT5

main.QML

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1

ApplicationWindow{
    visible:true
    width:640
    height:480
    id:window
    title: "editor"

    signal textUpdated(string text)


    TextArea{
        text:"hello"
        onTextChanged: textUpdated(text);
    }

}

有了QML文件之后,我们就可以根据该文件进行python文件的编写,把里面的信号传递给python文件中

mian.py

import sys
from PyQt5.QtCore import QObject, QUrl, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine

def show(text):
print(text)


if __name__ == "__main__":
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load('main.qml')
win = engine.rootObjects()[0]
win.textUpdated.connect(show)
win.show()
sys.exit(app.exec_())

这样,一个简单的python和Qml文件的通信的例子就完成了。