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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…