2017-08-23 27 views
0

我将一个按钮连接到绘制图形的方法。它按预期工作,但当我关闭图表窗口并单击按钮以再次显示图表时,没有任何反应。如何刷新,更新或断开PyQt5中的信号?

我试过refresh,updatedisconnect,但我找不到解决方案。我是PyQt的新手。

以下是我有:

import plot 
self.btn.clicked.connect(self.showPlot) 
def showPlot(self): 
     plot.plt.show() 

代码示例

剧情模块: plot.py

import numpy as np 
import matplotlib.pyplot as plt 

N = 5 
first_means = (20, 35, 30, 35, 27) 
first_std = (2, 3, 4, 1, 2) 

ind = np.arange(N) 
width = 0.35  

fig, ax = plt.subplots() 
rects1 = ax.bar(ind, first_means, width, color='r', yerr=first_std) 

second_means = (25, 32, 34, 20, 25) 
second_std = (3, 5, 2, 3, 3) 
rects2 = ax.bar(ind + width, second_means, width, color='y', yerr=second_std) 

PyQt5模块:

import sys 
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication) 
import plot 

class Example(QWidget): 

    def __init__(self): 
     super().__init__() 

     self.initUI() 

    def initUI(self):  

     self.btn = QPushButton('Show Plot', self) 
     self.btn.move(20, 20) 
     self.btn.clicked.connect(self.showPlot) 

     self.setGeometry(300, 300, 290, 150) 
     self.setWindowTitle('Show Plot') 
     self.show() 

    def showPlot(self): 
     plot.plt.show() 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    ex = Example() 
    sys.exit(app.exec_()) 
+0

请提供[MCVE](https://stackoverflow.com/help/mcve)。您发布的代码不完整,并不能证明问题。你不应该改变任何信号多次工作 – user3419537

+0

@ user3419537感谢您的评论。我认为我的原始问题被认定为MCVE,因为它描述了实际问题。但是,您说得对,另外一些示例代码可能会有所帮助。所以,我编辑了我的问题并发布了一个例子。 –

回答

1

当您关闭它消除matplotlib的应用程序的窗口,除了在另一个文件中有一个脚本是不是一个很好的做法,最好是只有函数,类和/或定义,所以我建议调整你的项目如下:

plot.py

import numpy as np 
import matplotlib.pyplot as plt 

def customplot(): 
    N = 5 
    first_means = (20, 35, 30, 35, 27) 
    first_std = (2, 3, 4, 1, 2) 

    ind = np.arange(N) 
    width = 0.35  

    fig, ax = plt.subplots() 
    rects1 = ax.bar(ind, first_means, width, color='r', yerr=first_std) 

    second_means = (25, 32, 34, 20, 25) 
    second_std = (3, 5, 2, 3, 3) 
    rects2 = ax.bar(ind + width, second_means, width, color='y', yerr=second_std) 
    plt.show() 

main.py

import sys 
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication) 
import plot 

class Example(QWidget): 

    def __init__(self): 
     super().__init__() 

     self.initUI() 

    def initUI(self):  
     self.btn = QPushButton('Show Plot', self) 
     self.btn.move(20, 20) 
     self.btn.clicked.connect(self.showPlot) 

     self.setGeometry(300, 300, 290, 150) 
     self.setWindowTitle('Show Plot') 
     self.show() 

    def showPlot(self): 
     plot.customplot() 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    ex = Example() 
    sys.exit(app.exec_()) 

诺塔:在我的情况下,你的代码永远不会奏效,因为窗口总是受阻,而不是用我的作品提出最佳的实施。