PyQt5 调用 View 视图的方法

一、使用Qt Designer

1、 直接引用ui文件:

from PyQt5.uic import loadUi

class MainWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = loadUi('demoLineEdit.ui', self)
        self.ui.ButtonClickMe.clicked.connect(self.message)
        self.show()

    def message(self):
        self.ui.labelResponse.setText("Hello " + self.ui.lineEditName.text())

2、将ui文件转为py文件:

pyuic5 demoLineEdit.ui -o demoLineEdit.py

然后引用demoLineEdit.py:

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from demoLineEdit import *
class MyForm(QDialog): def __init__(self): super().__init__() self.ui = Ui_Dialog() self.ui.setupUi(self) self.ui.ButtonClickMe.clicked.connect(self.message) self.show() def message(self): self.ui.labelResponse.setText("Hello "+self.ui.lineEditName.text())

二、使用qml文件

from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
import sys


def runQML():
    # material style
    sys.argv += ['--style', 'material']

    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.load('qmls/main.qml')

    if not engine.rootObjects():
        return -1
    return app.exec()


if __name__ == '__main__':
    runQML()

备注:使用material style也可以用这个方法https://github.com/eyllanesc/stackoverflow/tree/586e25c95bc5014874c58389c12947bf77037afc/PyQt%20Material%20Qml

 

--End--

转载于:https://www.cnblogs.com/ibgo/p/11447251.html

你可能感兴趣的:(PyQt5 调用 View 视图的方法)