Показаны различия между двумя версиями страницы.
Следующая версия | Предыдущая версия | ||
develop:pyton:pyqt [2022/05/08 11:35] admin создано |
develop:pyton:pyqt [2022/05/08 13:37] (текущий) admin |
||
---|---|---|---|
Строка 2: | Строка 2: | ||
- | ===== ===== | + | <code bash>pip install pyqt5 </ |
+ | |||
+ | ==== Примеры | ||
+ | |||
+ | < | ||
+ | < | ||
+ | <code python> | ||
+ | import sys | ||
+ | from PyQt5 import QtWidgets, | ||
+ | |||
+ | app = QtWidgets.QApplication(sys.argv) | ||
+ | windows | ||
+ | windows.resize(500, | ||
+ | windows.move(100, | ||
+ | |||
+ | windows.setWindowTitle(' | ||
+ | # set icon | ||
+ | windows.setWindowIcon(QtGui.QIcon(' | ||
+ | windows.show() | ||
+ | sys.exit(app.exec_()) | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ==== ==== | ||
+ | |||
+ | < | ||
+ | < | ||
+ | <code python> | ||
+ | import sys | ||
+ | import time | ||
+ | from PyQt5.QtCore import QThread, pyqtSignal | ||
+ | from PyQt5.QtWidgets import QWidget, QPushButton, | ||
+ | |||
+ | class Thread(QThread): | ||
+ | _signal = pyqtSignal(int) | ||
+ | def __init__(self): | ||
+ | super(Thread, | ||
+ | |||
+ | def __del__(self): | ||
+ | self.wait() | ||
+ | |||
+ | def run(self): | ||
+ | for i in range(100): | ||
+ | time.sleep(0.1) | ||
+ | self._signal.emit(i) | ||
+ | |||
+ | class Example(QWidget): | ||
+ | def __init__(self): | ||
+ | super(Example, | ||
+ | self.setWindowTitle(' | ||
+ | self.btn = QPushButton(' | ||
+ | self.btn.clicked.connect(self.btnFunc) | ||
+ | self.pbar = QProgressBar(self) | ||
+ | self.pbar.setValue(0) | ||
+ | self.resize(300, | ||
+ | self.vbox = QVBoxLayout() | ||
+ | self.vbox.addWidget(self.pbar) | ||
+ | self.vbox.addWidget(self.btn) | ||
+ | self.setLayout(self.vbox) | ||
+ | self.show() | ||
+ | |||
+ | def btnFunc(self): | ||
+ | self.thread = Thread() | ||
+ | self.thread._signal.connect(self.signal_accept) | ||
+ | self.thread.start() | ||
+ | self.btn.setEnabled(False) | ||
+ | |||
+ | def signal_accept(self, | ||
+ | self.pbar.setValue(int(msg)) | ||
+ | if self.pbar.value() == 99: | ||
+ | self.pbar.setValue(0) | ||
+ | self.btn.setEnabled(True) | ||
+ | |||
+ | if __name__ == " | ||
+ | app = QApplication(sys.argv) | ||
+ | ex = Example() | ||
+ | ex.show() | ||
+ | sys.exit(app.exec_()) | ||
+ | </ | ||
+ | </ | ||
+ | |||
==== ==== | ==== ==== | ||
+ | < | ||
+ | < | ||
+ | Подключение модулей нужно проверить, | ||
+ | |||
+ | <code python> | ||
+ | import sys, time | ||
+ | from PySide2 import QtWidgets | ||
+ | from PySide2.QtQml import QQmlApplicationEngine | ||
+ | from PySide2.QtCore import QObject, Slot | ||
+ | from PySide2.QtQuick import QQuickView | ||
+ | from PySide2.QtGui import QGuiApplication | ||
+ | |||
+ | class Messenger(QObject): | ||
+ | @Slot(str) | ||
+ | def PrintMessage(self, | ||
+ | print(' | ||
+ | |||
+ | app= QGuiApplication(sys.argv) | ||
+ | view= QQmlApplicationEngine(' | ||
+ | mes = Messenger() | ||
+ | |||
+ | ctx = view.rootContext() | ||
+ | ctx.setContextProperty(" | ||
+ | |||
+ | sys.exit(app.exec_()) | ||
+ | </ | ||
+ | |||
+ | <code qml> | ||
+ | import QtQuick 2.2 | ||
+ | import QtQuick.Window 2.1 | ||
+ | import QtQuick.Controls 1.2 | ||
+ | import QtQuick.Dialogs 1.1 | ||
+ | |||
+ | ApplicationWindow | ||
+ | { | ||
+ | visible: true; width: 360; height: 360 | ||
+ | | ||
+ | Rectangle { | ||
+ | id: button | ||
+ | width: 150; height: 40 | ||
+ | color: " | ||
+ | anchors.horizontalCenter: | ||
+ | y: 120 | ||
+ | MouseArea { | ||
+ | id: buttonMouseArea | ||
+ | objectName: " | ||
+ | anchors.fill: | ||
+ | onClicked: { | ||
+ | messenger.PrintMessage(" | ||
+ | } | ||
+ | } | ||
+ | Text { | ||
+ | id: buttonText | ||
+ | text: "Press me!" | ||
+ | anchors.horizontalCenter: | ||
+ | anchors.verticalCenter: | ||
+ | font.pointSize: | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ==== ==== | ||
+ | |||
+ | < | ||
+ | < | ||
+ | <code python> | ||
+ | |||
+ | </ | ||
+ | </ | ||