1- # -*- coding: utf-8 -*-
2- # @Author: Marcel Reis-Soubkovsky
3- # @Date: 2020-06-28 15:46:41
4- # @Last Modified by: Marcel Reis-Soubkovsky
5- # @Last Modified time: 2020-07-08 18:04:03
6-
7- # from PySide2.QtWidgets import *
8- from PySide2 .QtUiTools import QUiLoader
9- from PySide2 .QtCore import Qt , QFile , QIODevice
10- from PySide2 .QtWidgets import QApplication , QWidget , QFileDialog , QVBoxLayout , QGridLayout , QMainWindow
11- from PySide2 .QtGui import QIcon
12- import sys
13-
14- from matplotlib .backends .backend_qt5agg import (
15- FigureCanvas , NavigationToolbar2QT as NavigationToolbar )
1+ from PyQt5 import QtWidgets , QtGui
2+
3+ import matplotlib
4+ matplotlib .use ('Qt5Agg' )
165import matplotlib .pyplot as plt
176
7+ from matplotlib .backends .backend_qt5agg import FigureCanvasQTAgg
188from matplotlib .figure import Figure
19- from matplotlib .widgets import Slider , Button , RadioButtons
20- from cycler import cycler
21-
22- import numpy as np
239
24-
25- class MplWidget (QWidget ):
10+ class MplWidget (FigureCanvasQTAgg ):
2611
27- def __init__ (self , parent = None ):
28-
29- # super().__init__()
30- QWidget .__init__ (self , parent )
31-
32- self .mpl_canvas = FigureCanvas (Figure (constrained_layout = True , facecolor = '#f0f0f0' ))
33- self .mpl_canvas .axes = self .mpl_canvas .figure .add_subplot (111 )
34- # self.mpl_canvas.setParent()
35-
36- vertical_layout = QVBoxLayout ()
37- vertical_layout .addWidget (self .mpl_canvas )
38- self .setLayout (vertical_layout )
39- # vertical_layout.addWidget(NavigationToolbar(self.mpl_canvas, self)) #uncomment in order to have the matplotlib tools
12+ def __init__ (self , parent = None , width = 5 , height = 4 , dpi = 100 ):
4013
14+ fig = Figure (figsize = (width , height ), dpi = dpi )
15+ self .axes = fig .add_subplot (111 )
16+ super (MplWidget , self ).__init__ (fig )
4117
4218# --------------------------------------
43- class EasyGUI ( QWidget ):
19+ class EasyPlotGUI ( QtWidgets . QMainWindow ):
4420
4521 """Easy GUI with Matplotlib
4622 """
4723
48- def __init__ (self ):
24+ def __init__ (self , ui_python_file ):
25+ self .app = QtWidgets .QApplication ([])
4926 self .window_title = "EasyPlotGUI"
50- self .ui_filepath = "test_example.ui"
51- self .icon_path = "logo.png"
27+
28+ self .icon_path = None
29+ self .ui = ui_python_file .Ui_MainWindow ()
30+ super ().__init__ ()
31+ self .ui .setupUi (self )
5232
53-
5433
5534 def show_gui (self ):
56- """This is the core of the execution .
35+ """Shows the GUI .
5736 """
58- app = QApplication ([])
59-
60- QWidget .__init__ (self )
61-
62- loader = QUiLoader ()
63- loader .registerCustomWidget (MplWidget )
64- # self.ui = loader.load(designer_file, self)
65- ui_file = QFile (self .ui_filepath )
66- try :
67- ui_file .open (QIODevice .ReadOnly )
68- except :
69- raise TypeError ("Unable to open {}: {}" .format (self .ui_filepath , ui_file .errorString ()))
70- # loader = QUiLoader()
71-
72- self .ui = loader .load (ui_file , None )
73- ui_file .close ()
7437
7538 try :
76- self .ax = self .ui .MplWidget .mpl_canvas .axes
77- print ("après" )
78- self .canvas = self .ui .MplWidget .mpl_canvas
79-
80- custom_cycler = (cycler (color = ['#ed2124' ,'#808081' ,'c' , 'm' , 'y' , 'k' ]))
81- plt .rc ('axes' , prop_cycle = custom_cycler )
39+ self .ax = self .ui .MplWidget .axes
40+ self .draw = self .ui .MplWidget .draw
8241 self .update_graph ()
8342 except AttributeError :
84- # raise AttributeError("ERROR")
8543 pass
8644
87- if not self .ui :
88- print (loader .errorString ())
89- sys .exit (- 1 )
90-
9145 self .update_interactivity ()
9246
9347
9448 self .setWindowTitle (self .window_title )
9549
96-
97-
98- grid_layout = QGridLayout ()
99- grid_layout .addWidget (self .ui )
100- self .setLayout (grid_layout )
101-
102- self .QFileDialog = QFileDialog
103- self .QMainWindow = QMainWindow
104-
10550 if self .icon_path != None :
106- self .setWindowIcon (QIcon (self .icon_path ))
51+
52+ self .setWindowIcon (QtGui .QIcon (self .icon_path ))
10753
108- self .show ()
109- app .exec_ ()
54+ self .show () # Show a window
55+ self . app .exec_ ()
11056
11157 def update_interactivity (self ):
11258 """This method adds the interactivity between the GUI elements and the code. An example of execution can be seen below. This method is supposed to be overwritten when EasyGUI is imported as a parent class for adding your own graphs.
@@ -122,55 +68,5 @@ def update_graph(self):
12268
12369 """
12470 # example, to be overwritten
125- y = np .random .rand (100 )
126- x = np .linspace (0 ,1 ,100 )
127-
128- self .ax .clear ()
129- self .ax .plot (x , y , label = "Random" )
130- self .ax .legend ()
131- self .ax .set_title ('Random Values' )
132- self .canvas .draw ()
133-
134- if __name__ == "__main__" :
135- # Test Example
136- gui = EasyGUI ()
137- gui .window_title = "Window Title"
138- gui .ui_filepath = "test_example.ui"
139- gui .icon_path = "./logo.png"
140- gui .show_gui ()
141-
142- # EXAMPLE OF USAGE
143- # Slider changing the frequency of a sine wave.
144-
145- # class MyClass(EasyPlotGUI):
146- # def __init__(self):
147- # super().__init__()
148- # self.window_title="My GUI Name"
149- # self.ui_filepath="X:/xxxxx/xxxx/your_GUI.ui"
150- # self.icon_path="X:/xxxxx/xxxx/your_GUI_icon.png"
151-
152- # #initialize Graph variables for first plot
153- # self.f=1
15471
155- # def update_interactivity(self):
156- # self.ui.mySlider.valueChanged(self.change_frequency())
157-
158- # def change_frequency(self):
159- # self.f=self.ui.mySlider.value()
160- # self.update_graph()
161-
162- # def update_graph(self):
163- # x=np.linspace(0,1)
164- # y=np.sin(2*np.pi*self.f*x)
165-
166- # self.ax.clear()
167- # self.ax.plot(x, y, label="Sine")
168- # self.ax.legend()
169- # self.ax.set_title('Sine Wave')
170- # self.canvas.draw()
171-
172- # #calling it
173- # my_gui=MyClass()
174- # my_gui.show_gui()
175-
176-
72+ self .draw ()
0 commit comments