【问题标题】:Drawing graph in the dialogue box in Python在 Python 对话框中绘制图形
【发布时间】:2021-04-29 09:08:27
【问题描述】:

我尝试编写一个程序,使用 PyQt5 和 pyqtgraph 在对话框中绘制图形。该程序应该创建一个带有按钮的窗口。按下该按钮应该会产生另一个带有绘制图形的窗口。我写了这段代码:

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

编译代码后,第一个窗口正确弹出,但在按下按钮后,它会创建两个窗口 - 一个带有正确绘制的图形,一个为空。关闭空窗口后,应用崩溃。

有人能指出我做错了什么以及如何解决吗?谢谢。

【问题讨论】:

  • 你是如何创建你的 MainWindow 的?您永远不会创建它的实例。运行此代码时,它会立即退出。
  • 是的,应该在代码末尾添加 MainWindow()。我会编辑帖子。

标签: python pyqt5 pyqtgraph


【解决方案1】:

我修复了你的代码,详情请查看代码中的 cmets:

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

最重要的是:您不能创建两个 QApplication 实例!这将总是导致崩溃!
此外,我简化了您的 MainWindow 类,它现在继承自 QWidget 而不是创建一个作为成员。现在,当您更改 y 数组时,x 值数组将自动具有正确的值。
此代码经过测试并可以正常工作。

【讨论】:

  • 工作。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 2011-01-04
相关资源
最近更新 更多