Skip to content Skip to sidebar Skip to footer

Matplotlib - _tkinter.tclerror: Bad Screen Distance "320.0"

File 'main.py', line 52, in r2n(name) File 'C:\Users\Riki\Documents\Universita\Erasmus\Personalization and Metadata modeling 02817\Final Project\friends_follo

Solution 1:

Try converting your coordinates to an int before creating the canvas item. For example:

self._tkcanvas.create_image(int(w/2), int(h/2), image=self._tkphoto)

I appreciate this answer very much, as it helped me a lot; I wish I could add a separate answer, but I can't as it's closed - so posting an edit:

A solution that worked for me that didn't require changing the matplotlib library files, is to simply make a new class to override a method, the two problematic methods being __init__ and resize (and strangely, all I need is to overload resize, didn't even had to put in the fix there, and it started working for me?)

Anyways, take note that the below is copied from the Python2.7 Matplotlib - you're probably better off checking for your local matplotlib version first, and copying from there:

# copy of /usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py# with fix:classFigureCanvasTkAggFix(FigureCanvasTkAgg):
  def__init__(self, figure, master=None, resize_callback=None):
    matplotlib.backends.backend_tkagg.FigureCanvasAgg.__init__(self, figure)
    self._idle = True
    t1,t2,w,h = self.figure.bbox.bounds
    w, h = int(w), int(h)
    self._tkcanvas = tk.Canvas(
      master=master, width=w, height=h, borderwidth=4)
    self._tkphoto = tk.PhotoImage(
      master=self._tkcanvas, width=w, height=h)
    self._tkcanvas.create_image(int(w/2), int(h/2), image=self._tkphoto) # fix
    self._resize_callback = resize_callback
    self._tkcanvas.bind("<Configure>", self.resize)
    self._tkcanvas.bind("<Key>", self.key_press)
    self._tkcanvas.bind("<Motion>", self.motion_notify_event)
    self._tkcanvas.bind("<KeyRelease>", self.key_release)
    for name in"<Button-1>", "<Button-2>", "<Button-3>":
      self._tkcanvas.bind(name, self.button_press_event)
    for name in"<ButtonRelease-1>", "<ButtonRelease-2>", "<ButtonRelease-3>":
      self._tkcanvas.bind(name, self.button_release_event)
    for name in"<Button-4>", "<Button-5>":
      self._tkcanvas.bind(name, self.scroll_event)
    root = self._tkcanvas.winfo_toplevel()
    root.bind("<MouseWheel>", self.scroll_event_windows)
    self._master = master
    self._tkcanvas.focus_set()
    self.sourced = dict()
    defon_idle(*ignore):
      self.idle_event()
      returnTruedefresize(self, event):
    width, height = event.width, event.height
    printse("WH", width, height, "\n")
    if self._resize_callback isnotNone:
      self._resize_callback(event)
    # compute desired figure size in inches
    dpival = self.figure.dpi
    winch = width/dpival
    hinch = height/dpival
    self.figure.set_size_inches(winch, hinch)
    self._tkcanvas.delete(self._tkphoto)
    self._tkphoto = tk.PhotoImage(
      master=self._tkcanvas, width=width, height=height)
    self._tkcanvas.create_image(width/2,height/2,image=self._tkphoto)
    self.resize_event()
    self.show()

Post a Comment for "Matplotlib - _tkinter.tclerror: Bad Screen Distance "320.0""