2012-04-29 107 views
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工作。 突然之間發生了這個問題,但我的代碼應該沒問題,因爲以前工作(沒有任何變化,情節是可見的)。Matplotlib - _tkinter.TclError:錯誤的屏幕距離「320.0」

我認爲這是圖書館的問題,我該怎麼辦?

回答

1

嘗試在創建畫布項之前將座標轉換爲int。例如:

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

我很欣賞這個答案非常多,因爲這對我幫助很大;我希望我可以添加一個單獨的答案,但我不能因爲它已關閉 - 所以張貼編輯:

一個解決方案,爲我工作,不需要更改matplotlib庫文件,只是簡單地做一個新的類override a method,這兩個問題的方法是__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() 
+0

哦,非常感謝你的工作!真誠地,我發現非常奇怪的必要性去圖書館和修改它們,但它的工作完美:) – RiccardoBucchi