0
File "main.py", line 52, in <module>
    r2n(name)

  File "C:\Users\Riki\Documents\Universita\Erasmus\Personalization and Metadata modeling 02817\Final Project\friends_followers__redis_to_networkx.py", line 69, in r2n

    **nx.draw_spring(g,node_size=50,node_color='#32CD32',node_shape='o',edge_color='.1',with_labels=True,width=0.5)**

  File "C:\Python27\lib\site-packages\networkx-1.6-py2.7.egg\networkx\drawing\nx_pylab.py", line 876, in draw_spring
    draw(G,spring_layout(G),**kwargs)

  File "C:\Python27\lib\site-packages\networkx-1.6-py2.7.egg\networkx\drawing\nx_pylab.py", line 124, in draw
    cf=pylab.gcf()

  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 369, in gcf
    return figure()

  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 343, in figure
    **kwargs)

  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 81, in new_figure_manager
    canvas = FigureCanvasTkAgg(figure, master=window)

  File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 188, in __init__
    self._tkcanvas.create_image(w/2, h/2, image=self._tkphoto)

  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2198, in create_image
    return self._create('image', args, kw)

  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2189, in _create
    *(args + self._options(cnf, kw))))

**_tkinter.TclError: bad screen distance "320.0"**

嗨,我正在为 Windows 64 位开发 Python 2.7。突然出现了这个问题,但我的代码应该没问题,因为以前可以工作(没有任何变化,图是可见的)。

我认为这是图书馆的问题,我该怎么办?

4

1 回答 1

1

在创建画布项目之前尝试将坐标转换为 int。例如:

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

我非常感谢这个答案,因为它对我帮助很大;我希望我可以添加一个单独的答案,但我不能因为它已关闭 - 所以发布一个编辑:

一个不需要更改matplotlib库文件的对我有用的解决方案是简单地创建一个新类来覆盖一个方法,两个有问题的方法是__init__resize(奇怪的是,我需要的只是重载resize,甚至没有在那里进行修复,它开始为我工作?)

无论如何,请注意以下内容是从 Python2.7 Matplotlib 复制的——您最好先检查本地 matplotlib 版本,然后从那里复制:

# copy of /usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py
# with fix:
class FigureCanvasTkAggFix(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()
    def on_idle(*ignore):
      self.idle_event()
      return True
  def resize(self, event):
    width, height = event.width, event.height
    printse("WH", width, height, "\n")
    if self._resize_callback is not None:
      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()
于 2012-04-29T21:44:40.873 回答