2017-08-23 30 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_()) 

諾塔:在我的情況下,你的代碼永遠不會奏效,因爲窗口總是受阻,而不是用我的作品提出最佳的實施。