Add source example files
This commit is contained in:
45
source/plotting/gui/base-window.ui
Normal file
45
source/plotting/gui/base-window.ui
Normal file
@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MainWindow</class>
|
||||
<widget class="QMainWindow" name="MainWindow">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QPushButton" name="refresh_button">
|
||||
<property name="text">
|
||||
<string>Refresh</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>34</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar"/>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
63
source/plotting/gui/base_graph.py
Normal file
63
source/plotting/gui/base_graph.py
Normal file
@ -0,0 +1,63 @@
|
||||
import pandas as pd
|
||||
from PySide6.QtCore import QObject, Slot
|
||||
from PySide6.QtUiTools import QUiLoader
|
||||
from PySide6.QtWidgets import QWidget, QMainWindow, QApplication, QPushButton
|
||||
from matplotlib.axes import Axes
|
||||
from matplotlib.backends.backend_qt import FigureCanvasQT
|
||||
from matplotlib.backends.backend_qtcairo import FigureCanvasQTCairo
|
||||
from matplotlib.figure import Figure
|
||||
|
||||
|
||||
class Main(QObject):
|
||||
"""Classe de gestion d'affichage de diagramme dans pyside."""
|
||||
area: QWidget = None
|
||||
window: QMainWindow | QWidget = None
|
||||
canvas: FigureCanvasQT = None
|
||||
refresh_button: QPushButton = None
|
||||
plot1: Axes = None
|
||||
data = pd.DataFrame({"age": [25, 45, 65], "prenom": ["Pierre", "Paul", "Jacques"]})
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.window = QUiLoader().load("base-window.ui")
|
||||
self.area = self.window.centralWidget()
|
||||
self.refresh_button = self.window.refresh_button
|
||||
# Créer un canevas compatible avec Matplotlib
|
||||
self.canvas = FigureCanvasQTCairo()
|
||||
self.canvas.figure = self.create_figures()
|
||||
self.area.layout().addWidget(self.canvas)
|
||||
self.show_bar()
|
||||
# Répondre à un événement sur le bouton
|
||||
self.refresh_button.clicked.connect(self.on_refresh_button_click)
|
||||
|
||||
@Slot()
|
||||
def on_refresh_button_click(self):
|
||||
"""Rafraîchir le graphique."""
|
||||
self.show_line()
|
||||
self.canvas.draw()
|
||||
|
||||
def create_figures(self) -> Figure:
|
||||
"""Générer une image avec un graphique."""
|
||||
figure = Figure(dpi=96)
|
||||
self.plot1 = figure.add_subplot(111) # 1 colonne, 1 ligne, position 1
|
||||
return figure
|
||||
|
||||
def show_bar(self):
|
||||
"""Mettre à jour le canevas avec un diagramme en barres."""
|
||||
self.canvas.figure.clear() # retirer tous les graphiques
|
||||
self.plot1 = self.canvas.figure.add_subplot(111) # 1 colonne, 1 ligne, position 1
|
||||
self.data.plot.bar(x="prenom", y="age", ax=self.plot1, rot=0)
|
||||
|
||||
def show_line(self):
|
||||
"""Mettre à jour le canevas avec un diagramme en lignes."""
|
||||
self.canvas.figure.clear() # retirer tous les graphiques
|
||||
self.plot1 = self.canvas.figure.add_subplot(111) # 1 colonne, 1 ligne, position 1
|
||||
self.data.plot.line(x="prenom", y="age", ax=self.plot1, color="#ffa820")
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
application = QApplication()
|
||||
main = Main()
|
||||
main.window.show()
|
||||
application.exec()
|
44
source/plotting/gui/quick_threading.py
Normal file
44
source/plotting/gui/quick_threading.py
Normal file
@ -0,0 +1,44 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
|
||||
from PySide6.QtCore import QObject, Slot, QThreadPool
|
||||
from PySide6.QtUiTools import QUiLoader
|
||||
from PySide6.QtWidgets import QWidget, QMainWindow, QApplication, QPushButton
|
||||
|
||||
|
||||
class Main(QObject):
|
||||
"""Classe de gestion de threads dans Pyside."""
|
||||
area: QWidget = None
|
||||
window: QMainWindow | QWidget = None
|
||||
refresh_button: QPushButton = None
|
||||
thread_pool = QThreadPool()
|
||||
counter: int = 0
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.window = QUiLoader().load("base-window.ui")
|
||||
self.area = self.window.centralWidget()
|
||||
self.refresh_button = self.window.refresh_button
|
||||
# Répondre à un événement sur le bouton
|
||||
self.refresh_button.clicked.connect(self.on_refresh_button_click)
|
||||
|
||||
@Slot()
|
||||
def on_refresh_button_click(self):
|
||||
"""Rafraîchir le graphique."""
|
||||
self.refresh_button.setDisabled(True)
|
||||
self.thread_pool.start(self.counter_updater)
|
||||
|
||||
def counter_updater(self):
|
||||
"""Thread."""
|
||||
while True:
|
||||
self.counter += 1
|
||||
self.refresh_button.setText(f"{self.counter}")
|
||||
time.sleep(1.0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
application = QApplication()
|
||||
main = Main()
|
||||
main.window.show()
|
||||
application.exec()
|
Reference in New Issue
Block a user