简体   繁体   中英

How to refresh, update or disconnect signal in PyQt5?

I connected a button to a method which plots a graph. It works as expected, but when I close the graph window and click on the button to try to show the plot again, nothing happens.

I tried refresh , update and disconnect , but I couldn't find a solution. I am new to PyQt.

Here is what I have:

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

Code Example

Plot Module: 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 Module:

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_())

When you close the window it eliminates the application of matplotlib, besides having a script in another file is not a good practice, it is advisable to have only functions, classes and / or definitions, so I recommend to restructure your project to the following:

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_())

Nota: In my case, your code that never works, since the window was always blocked, instead with the implementation I propose works optimally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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