简体   繁体   English

PyQt5 程序中的意外小部件弹出,我不知道如何修复它

[英]Unintended widget popup in a PyQt5 program and I don't know how to fix it

So I was able to successfully embed matplotlib into my PyQt5 program, except I am running into a problem where it seems the code I have is causing a popup of a matplot widget to open and close during the generation of the matplot for the widget.因此,我能够成功地将 matplotlib 嵌入到我的 PyQt5 程序中,但我遇到了一个问题,我的代码似乎导致在生成小部件的 matplot 期间打开和关闭 matplot 小部件的弹出窗口。 I was able to source the problem, but I am stuck on how I can go about to fix it.我能够找到问题的根源,但我被困在如何解决问题上。

def getHexabinData(self, shotsDf):
    #returns the object type of the shot / makes hexabin
    shotsHex = plt.hexbin(-shotsDf.LOC_X, shotsDf.LOC_Y, 
        extent=(-250, 250, 422.5, -47.5), cmap='Blues', gridsize=45, marginals=True, visible=False)
    print('done')
    #grabs object of hexabin of all shots
    makeDf = shotsDf[shotsDf.SHOT_MADE_FLAG == 1]
    #grabs the data frame of all the makes
    makesHex = plt.hexbin(-makeDf.LOC_X, makeDf.LOC_Y,
        extent=(-250, 250, 422.5, -47.5), cmap=plt.cm.Reds, gridsize=45, marginals=True, visible=False)
    print('done')
    plt.close()
    #close the hexabin plot
    pctsByHex = np.true_divide(makesHex.get_array(), shotsHex.get_array())
    pctsByHex[np.isnan(pctsByHex)] = 0  # convert NAN values to 0
    sizesByHex = len(shotsHex.get_array()) * [0]
    sizesByHex = self.getSizeHexByZone(shotsDf, sizesByHex)
    sizesByHex = sizesByHex * 120
    #size 210 for figsize(12,11)
    print('hexes done')
    return shotsHex, pctsByHex, sizesByHex

And so, I've sourced the problem to be in the function above, which is a function of a separate class in a separate file that uses the following module instead of:因此,我发现问题出在上面的 function 中,这是一个单独的 class 的 function ,而不是使用以下模块的单独文件:

import matplotlib.pyplot as plt
#instead of these imported modules below for the pyqt5 program
from matplotlib.patches import Circle, Rectangle, Arc
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)

Apologies if this question is way too specific of a problem.如果这个问题过于具体,请道歉。 I've tried to do:我试过这样做:

plt.close()
plt.hexabin(....visible=False)

but I still get this random "matplot" widget popup that opens and closes itself until the matplot widget shows the updated plot.但我仍然得到这个随机的“matplot”小部件弹出窗口,它会自行打开和关闭,直到 matplot 小部件显示更新的 plot。 Is there any fix to this or something I am not seeing?有什么解决方法或我没有看到的东西吗?

Do not use import matplotlib.pyplot as plt when you integrate Matplotlib in PyQt.在 PyQt 中集成 Matplotlib 时,不要使用import matplotlib.pyplot as plt The pyplot module has its own event loop and maintains its own list of windoww. pyplot模块有自己的事件循环并维护自己的 windoww 列表。 This clashes with PyQt as you are now are experiencing.这与您现在正在经历的 PyQt 发生冲突。

So remove the plt.close() statement.所以删除plt.close()语句。 Instead just close the Qt window when needed.相反,只需在需要时关闭 Qt window。

A good example on how to integrate without using pyplot can be found here .可以在此处找到有关如何在不使用pyplot的情况下进行集成的一个很好的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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