matplotlib公司,节目()在另一个方法中=单击时没有

2024-04-29 07:40:46 发布

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

当我把节目()在另一种方法中,不可能单击按钮:

import matplotlib.pyplot as plt
from matplotlib.widgets import Button

class ButtonTest:
    def __init__(self):
        ax = plt.axes([0.81, 0.05, 0.1, 0.075])
        bnext = Button(ax, 'Next')
        bnext.on_clicked(self._next)
#         plt.show()

    def show(self):
        print("when i put plt.show() in a different method, it's impossible to click the button")
        plt.show()

    def _next(self, event):
        print("next !")

b = ButtonTest()
b.show()

当鼠标移到按钮上时,它甚至不会高亮显示。有人知道为什么和如何解决这个问题吗?你知道吗


Tags: 方法importselfmatplotlibdefshowbuttonplt
1条回答
网友
1楼 · 发布于 2024-04-29 07:40:46

现在的情况是,在显示绘图之前,button对象正在被垃圾收集。你得留个参考号。你知道吗

例如,如果你改变

bnext = Button(...)

self.bnext = Button(...)

一切都应该正常。你知道吗

作为一个完整的例子:

import matplotlib.pyplot as plt
from matplotlib.widgets import Button

class ButtonTest:
    def __init__(self):
        ax = plt.axes([0.81, 0.05, 0.1, 0.075])
        self.bnext = Button(ax, 'Next')
        self.bnext.on_clicked(self._next)

    def show(self):
        plt.show()

    def _next(self, event):
        print("next !")

ButtonTest().show()

相关问题 更多 >