0

For a project I needed to implement a tool that can intuitively adjust the contrast of an image. Eventually, I came up with a solution which you can find here. While this tool can certainly be improved on many levels, there is one particular thing that still irks me quite a bit. As I have pointed out in the other post, sometimes, when closing the window, I get the following error message:

Exception ignored in: <function WeakMethod.__new__.<locals>._cb at 0x00000193A3D7C7B8>
Traceback (most recent call last):
  File "C:\Users\mapf\Anaconda3\lib\weakref.py", line 58, in _cb
  File "C:\Users\mapf\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 182, in _remove_proxy
  File "C:\Users\mapf\Anaconda3\lib\weakref.py", line 74, in __eq__
TypeError: isinstance() arg 2 must be a type or tuple of types 

I have simplified my program as much as I could so that you would still receive the error. Here is the code:

import sys
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import \
    FigureCanvasQTAgg as FigureCanvas

from PyQt5.QtWidgets import QDialog, QApplication, QGridLayout


class Figure:
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setMaximumHeight(100)

        self.fig.canvas.mpl_connect('pick_event', self.on_pick_event)
        self.fig.canvas.mpl_connect(
            'button_release_event', self.on_release_event
        )
        self.fig.canvas.mpl_connect(
            'button_press_event', self.on_button_press_event
        )
        self.fig.canvas.mpl_connect(
            'motion_notify_event', self.on_motion_event
        )

        self.canvas.draw()

    def on_pick_event(self, _):
        # print('picked')
        pass

    def on_button_press_event(self, _):
        # print('pressed')
        pass

    def on_release_event(self, _):
        # print('released')
        pass

    def on_motion_event(self, _):
        # print('moved')
        pass


class MainWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.layout = QGridLayout(self)
        self.image = Figure()
        self.layout.addWidget(self.image.canvas, 1, 0)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    GUI = MainWindow()
    GUI.show()
    sys.exit(app.exec_())

Now, if I run the code and simply run my cursor over the figure (or click on it a couple of times) and immediately close the window afterwards by pressing the 'X'-button, I receive the error message about every other time. I have already pinpointed the cause to the event handling connections. I was able to reproduce the error whenever at least two of the connections are active.

Can this have something to do with some kind of overlap in how the events are handled, and maybe the fact that you use the mouse to close the window? I really don't understand this.

mapf
  • 1,906
  • 1
  • 14
  • 40

1 Answers1

0

This turned out to be a bug and was solved.

mapf
  • 1,906
  • 1
  • 14
  • 40