pyqt连接到按钮的另一个文件中的函数绘制图形

时间:2019-04-27 11:29:32

标签: python pyqt

我有一个以下代码,该代码使用matplotlib创建了一个图:

plot.py

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure


class Canvas(FigureCanvas):

    def __init__(self, parent = None, width=5, height=10, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        FigureCanvas.__init__(self, fig)

    def plot(self):
        print("im plotting")
        current_data = [46.67, 50.0, 47.06, 50.0]
        ax = self.figure.add_subplot(111)
        ax.scatter(current_data, current_data)
        print(current_data)

此Canvas类在另一个文件中使用:

guiwrapper.py

from PyQt5 import QtCore, QtGui, QtWidgets, uic
import driver
from plot import Canvas


baseUIClass, baseUIWidget = uic.loadUiType("mygui.ui")


class Logic(baseUIWidget, baseUIClass):
    def __init__(self, parent=None):
        super(Logic, self).__init__(parent)
        self.setupUi(self)

        canvas = Canvas(self, width=8, height=4)
        self.plot_location.addWidget(canvas)
        canvas.plot()
        #self.run_simulation.clicked.connect(canvas.plot)
        #self.run_simulation.clicked.connect(lambda: canvas.plot())

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = Logic(None)
    ui.show()
    sys.exit(app.exec_())

我根本无法弄清楚为什么在单击run_simulation按钮时无法绘制图形。如果我只是简单地编写canvas.plot(),则GUI可以很好地打开其中的图形,但是连接调用该函数的按钮将不起作用,该图形也不会被绘制且永远不会显示。

有人知道这里发生了什么吗?

1 个答案:

答案 0 :(得分:1)

尝试一下:

from PyQt5 import QtCore, QtGui, QtWidgets  #, uic
#import driver
#from plot import Canvas

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import random


class Canvas(FigureCanvas):
    def __init__(self, parent = None, width=5, height=10, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        FigureCanvas.__init__(self, fig)

    def plot(self):
        print("im plotting")
        current_data = random.sample(range(100), 4) #[46.67, 50.0, 47.06, 50.0]  # +
        ax = self.figure.add_subplot(111)
        ax.scatter(current_data, current_data)
        ax.set_title('Graphique')
        print(current_data)

#baseUIClass, baseUIWidget = uic.loadUiType("mygui.ui")


class Logic(QtWidgets.QWidget):         #(baseUIWidget, baseUIClass):
    def __init__(self, parent=None):
        super(Logic, self).__init__(parent)
#        self.setupUi(self)

        self.canvas = Canvas(self, width=8, height=4)
        self.plot_location = QtWidgets.QGridLayout(self)
        self.plot_location.addWidget(self.canvas)
        self.canvas.plot()

        self.run_simulation = QtWidgets.QPushButton("Update Graphique")       
        self.run_simulation.clicked.connect(self.canvasPlot)

        self.plot_location.addWidget(self.run_simulation)

    def canvasPlot(self):                                        # +++
        self.canvas.figure.clf()
        self.canvas.plot()
        # Draw Graph
        self.canvas.draw()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ui = Logic() #(None)
    ui.show()
    sys.exit(app.exec_())

enter image description here