如何在pyqtgraph中关闭窗口

2024-06-16 09:40:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想关闭我所有的pyqtgraph小部件。我遵循了这些建议,但它们没有起作用。这是我的密码

def makeWindows(amp, title):
    WYSIZE = 800
    WXSIZE = 800
    XSIZE = 200
    YSIZE = 200
    TSIZE = 100
    STEPS = np.array([0.0, 0.25, 0.5,.75, 1.0])

    first = "00007F"
    blue = "007FFF"
    cyan = "7FFF7F"
    yellow = "FF7F00"
    red = "7F0000"


    win = QtGui.QMainWindow()
    win.resize(WXSIZE, WYSIZE)
    CLRS = [first,blue, cyan, yellow, red]

    for i,item in enumerate(CLRS):
        CLRS[i] = list(ord(c) for c in item.decode('hex')) 
        CLRS[i].append(255)

    clrmp = pg.ColorMap(STEPS, np.array( CLRS))


    lut = clrmp.getLookupTable()

    plt = pg.PlotItem(labels={'bottom': ('samples', 'm'), 'left': ('stuff', 'm')}, title = title)
    plt.setAspectLocked(False)
    imv = pg.ImageView(view = plt)
    win.setCentralWidget(imv)
    #imv.setLevels(3,6)
    imv.ui.histogram.gradient.setColorMap(clrmp)
    imv.setImage(amp)
    win.show()

    return win, imv


def main():
    app = QtGui.QApplication([])


    win1, imv1 = makeWindows(amp, "amp")

    if __name__ == '__main__':
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):            
            status = QtGui.QApplication.instance().exec_()

            #sys.exit(status)
            imv1.close()
            win1.close()
            app.closeAllWindows()

main()

在我执行这个窗口之后,所有的窗口都应该关闭,但是它们没有。我甚至都没把照片关上。在

谢谢你的帮助

更新:

我的初衷是创建一种方法,允许用户在插入键盘中断时关闭所有窗口(ctrl-c)

我添加了以下函数

^{pr2}$

并在makeWindows的末尾添加了以下行

sh = QtGui.QShortcut(QtGui.QKeySequence("Ctrl+C"),imv,None, close_all)
sh.setContext(QtCore.Qt.ApplicationShortcut)

这使得当一个窗口处于焦点时,只要用户按Ctrl+c,它就会关闭所有窗口


Tags: closetitlemaindefpltwinamppg
1条回答
网友
1楼 · 发布于 2024-06-16 09:40:28

QApplication.exec_()的调用在应用程序退出之前不会返回块(在本例中,当您关闭窗口时,应用程序退出)。因此,在关闭窗口之前,不会调用关闭窗口的行。在

相关问题 更多 >