python - How to implement a QPushbutton to emit pyqt signal and call another class? -
i'm trying learn basic usage of emitting/receiving signals , i'm running trouble.
i wanted start basic created mainwindow , put qpushbutton called "plot".
when button pushed takes arguments (self, xdata, ydata) , runs method initiateplot class mainwindow(qmainwindow)
self.plotbtn = qpushbutton("plot") self.plotbtn.clicked.connect(partial(self.initiateplot, xdata, ydata)) def initiateplot(self,x,y): plotsignal = pyqtsignal(list, list) self.plotsignal.emit(x, y)
afterwards tried connect plotter class, "process finished exit code 1" believe error coming particular line of code in below class.
self.plotsignal.connect(self.plotdata)
code plotter class
class createfig(figurecanvas): def __init__(self): #super().__init__(figure(tight_layout=true)) self.figure = plt.figure() self.axes = self.figure.add_subplot(111) self.name = "" # xdata = [1,2,3,4,5] # ydata = [12,4,56,78,9] plt.figure() self.axes.set_xlabel('x label') #self.plotdata(xdata,ydata) self.plotsignal.connect(self.plotdata) def plotdata(self, xdata,ydata): print("plotting") self.axes.plot(xdata, ydata) self.draw() plt.show()
sorry whitespace might little messed up.
the current issue how go receiving signal emit initiateplot method.
the end goals able click plot button , create new window new plot figure each time.
if there easier way link button in mainwindow plotting class helpful.
here link full code: https://github.com/silvuurleaf/interactivegraphing/blob/master/livegraphing.py
as said, i'm not sure if looking for, following code should create new matplotlib window, each time button pressed.
import sys #importing pyqt5 library construct widgets graphic user interface (gui) application pyqt5 import qtcore, qtgui, qtwidgets pyqt5.qtgui import (qlineedit, qpushbutton, qslider, qapplication, qvboxlayout, qhboxlayout, qwidget, qlabel, qcheckbox, qradiobutton, qplaintextedit, qsizepolicy, qmainwindow,qframe, qfiledialog, qtablewidgetitem, qtablewidget, qmenu, qmessagebox, qaction, qtoolbar) pyqt5.qtcore import qt, qabstracttablemodel, pyqtsignal functools import partial import matplotlib matplotlib.use("qt5agg") matplotlib import pyplot plt plt.style.use(['ggplot']) class createfig(): def __init__(self): self.figure = plt.figure() self.axes = self.figure.add_subplot(111) self.name = "" self.axes.set_xlabel('x label') def plotdata(self, xdata,ydata): print("plotting") self.axes.plot(xdata, ydata) plt.show() class mainwindow(qmainwindow): def __init__(self): super(mainwindow, self).__init__() self.setwindowtitle("perspective") self.initializeui() def initializeui(self): xdata = [1,2,3,4,5] ydata = [12,4,56,78,9] #main widget self.main_widget = qwidget(self) self.setcentralwidget(self.main_widget) self.plotbtn = qpushbutton("plot") self.plotbtn.clicked.connect(partial(self.initiateplot, xdata, ydata)) self.hmain = qhboxlayout(self.main_widget) self.vboxleft = qvboxlayout() self.vboxleft.addwidget(self.plotbtn) self.hbox1 = qhboxlayout() self.vboxleft.addlayout(self.hbox1) self.plotlayout = qvboxlayout() self.hmain.addlayout(self.vboxleft) self.hmain.addlayout(self.plotlayout) self.show() def initiateplot(self,x,y): print("emit signal") f = createfig() f.plotdata(x, y) def main(): # main loop app = qapplication(sys.argv) # instance window = mainwindow() window.show() # appwindow = mainwindow() sys.exit(app.exec_()) if __name__ == "__main__": main()
Comments
Post a Comment