i'm using Pyqt5 to build a GUI. There is a way to call a function, defined in one class, from another function defined in a second class? As example i have my first class where i define the layout of GUI, and where i placed some buttons and a Canvas where to plot a graph. In the same class i defined the function connected to the button. Anyway, when i press the button i would like to call another function, defined in a second class.
First class
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "My_GUI"
self.top = 100
self.left = 100
self.width = 1680
self. height = 880
self.InitWindow()
# Definition of buttons
def InitWindow(self):
m = PlotCanvas(self, width=5, height=4)
m.move(300, 50)
self.btn1 = QPushButton("execute", self)
self.btn1.setGeometry(20, 410, 150, 50)
self.btn1.clicked.connect(self.execute)
def execute(self):
PlotCanvas.plot(self)
Second class where i define a Canvas to update a plot when the button execute is pressed
class PlotCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
# self.plot()
def plot(self):
data = [random.random() for i in range(25)]
ax = self.figure.add_subplot(111)
ax.plot(data, 'r-')
self.draw()
When i run the code, python crash. I'm using Pycharm