Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新PyQt小部件_Python_Matplotlib_Pyqt - Fatal编程技术网

Python 更新PyQt小部件

Python 更新PyQt小部件,python,matplotlib,pyqt,Python,Matplotlib,Pyqt,我有一个Python程序,可以使用自定义matplotlib小部件绘制简单的图形。 我的代码如下: import sys from GUI import * import random import matplotlib.pyplot as plt class GUIForm(QtGui.QDialog): def __init__(self, parent=None): QtGui.QWidget.__init__(self,parent) self

我有一个Python程序,可以使用自定义matplotlib小部件绘制简单的图形。 我的代码如下:

import sys
from GUI import *
import random
import matplotlib.pyplot as plt

class GUIForm(QtGui.QDialog):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        QtCore.QObject.connect(self.ui.pushButton,
                               QtCore.SIGNAL('clicked()'), self.PlotFunc)

    def PlotFunc(self):
        randValList = random.sample(range(0, 10), 10)
        print(randValList)
        self.ui.PlotWidget.canvas.ax.clear()
        self.ui.PlotWidget.canvas.ax.plot(randValList)

    def callFunc(self):
        myapp.PlotFunc()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = GUIForm()
    myapp.show()
    sys.exit(app.exec_())
当我运行程序时,我可以看到GUI,但当我单击按钮时,绘图不会显示线条。空的绘图是可见的。 然而,如果我这样做

def __init__(self, parent=None):
            QtGui.QWidget.__init__(self,parent)
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)

            QtCore.QObject.connect(self.ui.pushButton,
                                   QtCore.SIGNAL('clicked()'), self.PlotFunc)
            self.ui.PlotWidget.canvas.ax.plot(randValList)

程序绘制图形。 所以我猜这与sys.exitapp.exec有关,但我不知道如何解决这个问题。 感谢您的帮助


谢谢。

如果您在PlotFunc的末尾添加这一行,是否有效

self.ui.PlotWidget.canvas.draw()

如果在PlotFunc的末尾添加这一行,是否可行

self.ui.PlotWidget.canvas.draw()

您能确认一下您的按钮实例的名称吗?在连接函数中指定self.ui.butdown。通常,设计器使用self.butdown作为默认值创建它们。@dex19dt谢谢。它是self.ui,因为按钮是ui\u对话框的一部分。单击按钮时是否显示打印语句?你确认信号至少发出了吗?@jdi谢谢jdi。是的,我可以看到打印报表工作。每次单击按钮都会打印随机值。请确认按钮实例的名称好吗?在连接函数中指定self.ui.butdown。通常,设计器使用self.butdown作为默认值创建它们。@dex19dt谢谢。它是self.ui,因为按钮是ui\u对话框的一部分。单击按钮时是否显示打印语句?你确认信号至少发出了吗?@jdi谢谢jdi。是的,我可以看到打印报表工作。我每次点击按钮都会打印随机值。非常感谢!它确实奏效了。我不知道每次我画东西的时候都需要在画布上画画。我认为app_exec运行一个循环,检测每个输入,然后自动绘制。非常感谢。我发现窗口有一个重绘方法,没有参数。谢谢大家,非常感谢!它确实奏效了。我不知道每次我画东西的时候都需要在画布上画画。我认为app_exec运行一个循环,检测每个输入,然后自动绘制。非常感谢。我发现窗口有一个重绘方法,没有参数。谢谢大家。