简体   繁体   English

如何实现QPushbutton发出pyqt信号并调用另一个类?

[英]How to implement a QPushbutton to emit pyqt signal and call another class?

I'm trying to learn the basic usage of emitting/receiving signals and I'm running into some trouble. 我正在尝试学习发射/接收信号的基本用法,但遇到了一些麻烦。

I wanted to start with something basic so I created a MainWindow and put a QPushButton called "plot". 我想从一个基本的东西开始,所以我创建了一个MainWindow并放置了一个名为“ plot”的QPushButton。

When the button is pushed it takes the arguments (self, xdata, ydata) and runs the method initiatePlot from my class MainWindow(QMainWindow) 按下按钮时,它接受参数(自身,xdata,ydata)并从我的类MainWindow(QMainWindow)运行方法initializePlot

    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 I tried to connect it to my plotter class, but I just get "Process finished exit code 1" I believe the error to be coming from this particular line of code in my below class. 之后,我尝试将其连接到我的绘图仪类,但是我只是得到“处理完成的退出代码1”,我相信错误是由于我的下面的类中的这一行代码引起的。

self.PlotSignal.connect(self.plotData)

CODE FOR 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 the whitespace might be a little messed up. 抱歉,空白可能有点混乱。

The current issue is how I go about receiving the signal I emit from my initiatePlot method. 当前的问题是我如何接收从initializePlot方法发出的信号。

The end goals is to be able to click the plot button and create a new window with a new plot figure each time. 最终目标是能够每次单击绘图按钮并创建一个具有新绘图数字的新窗口。

If there is an easier way to link the button in my mainwindow to plotting class that would be helpful. 如果有一种更简单的方法可以将主窗口中的按钮链接到绘图类,这将很有帮助。

Here is a link to my full code: https://github.com/Silvuurleaf/InteractiveGraphing/blob/master/LiveGraphing.py 这是我完整代码的链接: https : //github.com/Silvuurleaf/InteractiveGraphing/blob/master/LiveGraphing.py

As said, I'm not sure if this is really what you are looking for, but the following code should create a new matplotlib window, each time the button is pressed. 如前所述,我不确定这是否是您真正要寻找的东西,但是每次按下按钮时,以下代码应创建一个新的matplotlib窗口。

import sys
#Importing PyQt5 library to construct widgets for Graphic User Interface (GUI) application
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import (QLineEdit, QPushButton, QSlider, QApplication, QVBoxLayout, QHBoxLayout,
                             QWidget, QLabel, QCheckBox, QRadioButton, QPlainTextEdit, QSizePolicy,
                             QMainWindow,QFrame, QFileDialog, QTableWidgetItem, QTableWidget, QMenu, QMessageBox,
                             QAction, QToolBar)
from PyQt5.QtCore import Qt, QAbstractTableModel, pyqtSignal

from functools import partial
import matplotlib
matplotlib.use("Qt5Agg")

from matplotlib import pyplot as 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM