Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
894 views
in Technique[技术] by (71.8m points)

pyqt5 - Drawing graph in the dialogue box in Python

I tried to write a program that uses PyQt5 and pyqtgraph to draw a graph in a dialogue box. That program should create a window with a button. Pressing that button should produce another window with a drawn graph. I wrote this code:

import PyQt5.QtWidgets as pq
import pyqtgraph as pg

class MainWindow():
    def __init__(self):
        self.app = pq.QApplication([])
        self.window = pq.QWidget()
        self.window.setFixedSize(500,300)
        self.layout = pq.QGridLayout()

        self.button_manual = pq.QPushButton('Draw')
        self.button_manual.clicked.connect(self.draw_manual)

        self.layout.addWidget(self.button_manual)
        self.window.setLayout(self.layout)

        self.window.show()
        self.app.exec_()

    def draw_manual(self):
        x = [1,2,3,4,5,6,7,8,9,10]
        y = [30,32,34,32,33,31,29,32,35,45]
        dialog = Dialog(x,y)
        dialog.exec_()

class Dialog(pq.QDialog):
    def __init__(self,x,y):
        super(Dialog,self).__init__()
        self.app = pq.QApplication([])
        self.main = draw_graph(x,y)
        self.main.show()

class draw_graph(pq.QMainWindow):
    def __init__(self,x,y):
        pq.QMainWindow.__init__(self)
        self.graphWidget = pg.PlotWidget()
        self.setCentralWidget(self.graphWidget)
        self.graphWidget.plot(x,y)

MainWindow()

After compiling the code first window pops up correctly, but after pressing the button it creates two windows - one with the correctly drawn graph and one empty. After closing the empty window, app crashes.

Could someone point out what am I doing wrong and how to fix it? Thanks.

question from:https://stackoverflow.com/questions/65886843/drawing-graph-in-the-dialogue-box-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I fixed your code, see the comments in the code for more details:

import PyQt5.QtWidgets as pq
import pyqtgraph as pg

class MainWindow(pq.QWidget):
    def __init__(self):
        pq.QWidget.__init__(self)
        self.setFixedSize(500, 300)
        self.layout = pq.QGridLayout()
        
        self.button_manual = pq.QPushButton('Draw')
        self.button_manual.clicked.connect(self.draw_manual)
        
        self.layout.addWidget(self.button_manual)
        self.setLayout(self.layout)
        
        self.show()
    
    def draw_manual(self):
        y = [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
        x = range(1, len(y) + 1) # when you change y values, x will automatically have the correct x values
        dialog = Dialog(x, y) # create the dialog ...
        dialog.exec_() # ... and show it

class Dialog(pq.QDialog):
    def __init__(self, x, y):
        pq.QDialog.__init__(self)
        self.layout = pq.QHBoxLayout() # create a layout to add the plotwidget to
        self.graphWidget = pg.PlotWidget()
        self.layout.addWidget(self.graphWidget) # add the widget to the layout
        self.setLayout(self.layout) # and set the layout on the dialog
        self.graphWidget.plot(x, y)

if __name__ == "__main__": # if run as a program, ad not imported ...
    app = pq.QApplication([]) # create ONE application (in your original code you had two, thats why it crashed)
    win = MainWindow() # create the main window
    app.exec_() # run the app

The most important : You cannot create two instances of QApplication ! That will always result in a crash !
Further, i simplified your MainWindow class, it now inherits from QWidget instead of creating one as a member. And the x values array will now have the correct values automatically, when you change the y array.
This code is tested and works as it should.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...