Инструменты пользователя

Инструменты сайта


develop:pyton:pyqt

Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Следующая версия
Предыдущая версия
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> 
 + 
 +==== Примеры  ==== 
 + 
 +<details> 
 +<summary>:!: Простое окно</summary> 
 +<code python> 
 +import sys 
 +from PyQt5 import QtWidgets,QtGui 
 + 
 +app QtWidgets.QApplication(sys.argv) 
 +windows QtWidgets.QWidget() 
 +windows.resize(500,500) 
 +windows.move(100,100) 
 + 
 +windows.setWindowTitle('Title'
 +# set icon 
 +windows.setWindowIcon(QtGui.QIcon('icon.png')) 
 +windows.show() 
 +sys.exit(app.exec_()) 
 +</code> 
 +</details> 
 + 
 + 
 + 
 +====  ==== 
 + 
 +<details> 
 +<summary>:!: Поток, прогресс бар </summary> 
 +<code python> 
 +import sys 
 +import time 
 +from PyQt5.QtCore import QThread, pyqtSignal 
 +from PyQt5.QtWidgets import QWidget, QPushButton, QProgressBar, QVBoxLayout, QApplication 
 + 
 +class Thread(QThread): 
 +    _signal = pyqtSignal(int) 
 +    def __init__(self): 
 +        super(Thread, self).__init__() 
 + 
 +    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).__init__() 
 +        self.setWindowTitle('QProgressBar'
 +        self.btn = QPushButton('Click me') 
 +        self.btn.clicked.connect(self.btnFunc) 
 +        self.pbar = QProgressBar(self) 
 +        self.pbar.setValue(0) 
 +        self.resize(300, 100) 
 +        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, msg): 
 +        self.pbar.setValue(int(msg)) 
 +        if self.pbar.value() == 99: 
 +            self.pbar.setValue(0) 
 +            self.btn.setEnabled(True) 
 + 
 +if __name__ == "__main__": 
 +    app = QApplication(sys.argv) 
 +    ex = Example() 
 +    ex.show() 
 +    sys.exit(app.exec_()) 
 +</code> 
 +</details> 
 + 
  
 ==== ==== ==== ====
  
 +<details>
 +<summary>:!: Форма Qml, связь сигнало-слота</summary>
 +Подключение модулей нужно проверить, есть лишние
 +
 +<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, message):
 +        print('Your message: "'+ message +'"')
 +
 +app= QGuiApplication(sys.argv)
 +view= QQmlApplicationEngine('win.qml')
 +mes = Messenger()
 +
 +ctx = view.rootContext()
 +ctx.setContextProperty("messenger", mes)
 +
 +sys.exit(app.exec_())
 +</code>
 +
 +<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: "darkgray"
 +        anchors.horizontalCenter: page.horizontalCenter
 +        y: 120
 +        MouseArea {
 +            id: buttonMouseArea
 +            objectName: "buttonMouseArea"
 +            anchors.fill: parent
 +            onClicked: {
 +                messenger.PrintMessage("da suka daaaa sukaaa")
 +            }
 +        }
 +        Text {
 +            id: buttonText
 +            text: "Press me!"
 +            anchors.horizontalCenter: button.horizontalCenter
 +            anchors.verticalCenter: button.verticalCenter
 +            font.pointSize: 16
 +        }
 +    }
 +}
 +</code>
 +</details>
 +
 +
 +
 +==== ====
 +
 +<details>
 +<summary>:!: </summary>
 +<code python>
 +
 +</code>
 +</details>
  
develop/pyton/pyqt.1652009728.txt.gz · Последнее изменение: 2022/05/08 11:35 — admin