Monday, May 30, 2022

Embedding custom widgets from Qt Designer on Ubuntu 20.04.4 Python 3.8

Выполните установку

$ sudo apt install qttools5-dev-tools 

Работая в QT5 Designer, следуйте официальным директивам

1.Добавьте виджет в главное окно. Назовите виджет как "graphWidget"

2. Продвиньте виджет, указав имя класса как PlotWidget и файл заголовка как pyqtgraph.
















Сохраните mainwindow.ui в рабочий каталог с виртуальным окружением содержащим пакеты для всех импортов в коде Пайтон , приведенных ниже ( через `pip install` )

(.env) boris@boris-All-Series:~/PYQT5$ cat firstApp.py

from PyQt5 import QtWidgets, uic

from pyqtgraph import PlotWidget

import pyqtgraph as pg

import sys

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):

        super(MainWindow, self).__init__(*args, **kwargs)

        #Load the UI Page

        uic.loadUi('mainwindow.ui', self)

def main():

    app = QtWidgets.QApplication(sys.argv)

    main = MainWindow()

    main.show()

    sys.exit(app.exec_())

if __name__ == '__main__':

    main()

(.env) boris@boris-All-Series:~/PYQT5$ cat secondApp.py

from PyQt5 import QtWidgets, uic

from pyqtgraph import PlotWidget, plot

import pyqtgraph as pg

import sys  # We need sys so that we can pass argv to QApplication

import os

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):

        super(MainWindow, self).__init__(*args, **kwargs)

        #Load the UI Page

        uic.loadUi('mainwindow.ui', self)

        self.plot([1,2,3,4,5,6,7,8,9,10], [30,32,34,32,33,31,29,32,35,45])

    def plot(self, hour, temperature):

        self.graphWidget.plot(hour, temperature)

def main():

    app = QtWidgets.QApplication(sys.argv)

    main = MainWindow()

    main.show()

    sys.exit(app.exec_())

if __name__ == '__main__':

    main()






























(.env) boris@boris-All-Series:~/PYQT5$ cat parabolApp.py
from PyQt5 import QtWidgets, uic
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import numpy as np
import sys  # We need sys so that we can pass argv to QApplication
import os

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        #Load the UI Page
        uic.loadUi('mainwindow.ui', self)

        np.random.seed(19680801)
        # create random data
        xdata = np.random.random([2, 50])        
        # split the data into two parts
        xdata1 = xdata[0, :]
        xdata2 = xdata[1, :]

        # sort the data so it makes clean curves
        xdata1.sort()
        xdata2.sort()

        # create some y data points
        ydata1 = xdata1 ** 2 
        ydata2 = 1 - xdata2 ** 3

        # Graphs plotted 
        self.plot(xdata1, ydata1)
        self.plot(xdata2, ydata2)

    def plot(self, hour, temperature):
        self.graphWidget.plot(hour, temperature)

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
































(.env) boris@boris-All-Series:~/PYQT5$ cat sinCoslApp.py
from PyQt5 import QtWidgets, uic
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import sys  # We need sys so that we can pass argv to QApplication
import os
import numpy as np

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        #Load the UI Page
        uic.loadUi('mainwindow.ui', self)
        
        x = np.arange(0,9 * np.pi, 0.1) 
        y = np.cos(np.cos(np.cos(np.cos(x)))) -  np.sin(np.sin(np.sin(np.sin(x)))) 

        self.plot(x, y) # plot grah 

    def plot(self, hour, temperature):
        self.graphWidget.plot(hour, temperature)

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
































No comments:

Post a Comment