Matplotlib-“u tkinter.TclError:屏幕距离不正确”;320.0“; 文件“main.py”,第52行,在 r2n(姓名) r2n中第69行的文件“C:\Users\Riki\Documents\Universita\Erasmus\Personalization and Metadata modeling 02817\Final Project\friends\u followers\u\u redis\u to\u networkx.py” **nx.draw_spring(g,节点大小=50,节点颜色=32CD32,节点形状=o,边颜色=0.1,带标签=True,宽度=0.5)** 文件“C:\Python27\lib\site packages\networkx-1.6-py2.7.egg\networkx\drawing\nx\u pylab.py”,第876行,位于draw\u spring中 图纸(G,弹簧布置图(G),**kwargs) 文件“C:\Python27\lib\site packages\networkx-1.6-py2.7.egg\networkx\drawing\nx\u pylab.py”,绘图中第124行 cf=pylab.gcf() gcf中第369行的文件“C:\Python27\lib\site packages\matplotlib\pyplot.py” 返回数字() 文件“C:\Python27\lib\site packages\matplotlib\pyplot.py”,第343行,如图所示 **kwargs) 文件“C:\Python27\lib\site packages\matplotlib\backends\backend\u tkagg.py”,第81行,在新的图形管理器中 canvas=FigureCanvasTkAgg(图,主视图=窗口) 文件“C:\Python27\lib\site packages\matplotlib\backends\backend\u tkagg.py”,第188行,在uu init中__ self.\u tkcanvas.创建图像(w/2,h/2,图像=self.\u tkphoto) 文件“C:\Python27\lib\lib-tk\Tkinter.py”,第2198行,在create_图像中 返回自我。\u创建('image',args,kw) 文件“C:\Python27\lib\lib tk\Tkinter.py”,第2189行,在创建 *(参数+自选项(cnf,kw))) **_tkinter.TclError:错误的屏幕距离“320.0”**
嗨,我正在为Windows64位开发Python2.7。 突然,这个问题发生了,但我的代码应该是正常的,因为以前工作过(没有任何更改,绘图是可见的)Matplotlib-“u tkinter.TclError:屏幕距离不正确”;320.0“; 文件“main.py”,第52行,在 r2n(姓名) r2n中第69行的文件“C:\Users\Riki\Documents\Universita\Erasmus\Personalization and Metadata modeling 02817\Final Project\friends\u followers\u\u redis\u to\u networkx.py” **nx.draw_spring(g,节点大小=50,节点颜色=32CD32,节点形状=o,边颜色=0.1,带标签=True,宽度=0.5)** 文件“C:\Python27\lib\site packages\networkx-1.6-py2.7.egg\networkx\drawing\nx\u pylab.py”,第876行,位于draw\u spring中 图纸(G,弹簧布置图(G),**kwargs) 文件“C:\Python27\lib\site packages\networkx-1.6-py2.7.egg\networkx\drawing\nx\u pylab.py”,绘图中第124行 cf=pylab.gcf() gcf中第369行的文件“C:\Python27\lib\site packages\matplotlib\pyplot.py” 返回数字() 文件“C:\Python27\lib\site packages\matplotlib\pyplot.py”,第343行,如图所示 **kwargs) 文件“C:\Python27\lib\site packages\matplotlib\backends\backend\u tkagg.py”,第81行,在新的图形管理器中 canvas=FigureCanvasTkAgg(图,主视图=窗口) 文件“C:\Python27\lib\site packages\matplotlib\backends\backend\u tkagg.py”,第188行,在uu init中__ self.\u tkcanvas.创建图像(w/2,h/2,图像=self.\u tkphoto) 文件“C:\Python27\lib\lib-tk\Tkinter.py”,第2198行,在create_图像中 返回自我。\u创建('image',args,kw) 文件“C:\Python27\lib\lib tk\Tkinter.py”,第2189行,在创建 *(参数+自选项(cnf,kw))) **_tkinter.TclError:错误的屏幕距离“320.0”**,python,windows,matplotlib,tkinter,64-bit,Python,Windows,Matplotlib,Tkinter,64 Bit,嗨,我正在为Windows64位开发Python2.7。 突然,这个问题发生了,但我的代码应该是正常的,因为以前工作过(没有任何更改,绘图是可见的) 我认为这是库的问题,我该怎么办?在创建画布项目之前,尝试将坐标转换为int。例如: File "main.py", line 52, in <module> r2n(name) File "C:\Users\Riki\Documents\Universita\Erasmus\Personalization and Meta
我认为这是库的问题,我该怎么办?在创建画布项目之前,尝试将坐标转换为int。例如:
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"**
我非常感激这个答案,因为它帮助了我很多;我希望我可以添加一个单独的答案,但我不能,因为它已关闭-因此发布编辑: 对我来说,一个不需要更改
matplotlib
库文件的解决方案是简单地为创建一个新类,两个有问题的方法是\uuuu init\uuu
和resize
(奇怪的是,我所需要的只是重载调整大小
,甚至不需要在那里进行修复,它就开始对我起作用了?)
无论如何,请注意以下内容是从Python2.7 Matplotlib复制的-您最好先检查本地Matplotlib版本,然后从那里复制:
self._tkcanvas.create_image(int(w/2), int(h/2), image=self._tkphoto)
#/usr/lib/pymodules/python2.7/matplotlib/backends/backend的副本
#使用修复程序:
类别FigureCastKaggFix(FigureCastKagg):
定义初始化(self,figure,master=None,resize\u callback=None):
matplotlib.backends.backend\u tkagg.FigureCanvasAgg.\uuuuuu init\uuuuuu(self,figure)
self.\u idle=True
t1,t2,w,h=self.figure.bbox.bounds
w、 h=整数(w),整数(h)
self.\u tkcanvas=tk.Canvas(
母版=母版,宽度=w,高度=h,边框宽度=4)
self.\u tkphoto=tk.PhotoImage(
主控=自控。_tkcanvas,宽度=w,高度=h)
self.\u tkcanvas.创建图像(int(w/2),int(h/2),图像=self.\u tkphoto)#修复
self.\u resize\u callback=resize\u callback
self.\u tkcanvas.bind(“,self.resize)
self.\u tkcanvas.bind(“,self.key\u press)
self.\u tkcanvas.bind(“,self.motion\u notify\u事件)
self.\u tkcanvas.bind(“,self.key\u release)
对于“”中的名称,请执行以下操作:
self.\u tkcanvas.bind(名称、self.button\u press\u事件)
对于“”中的名称,请执行以下操作:
self.\u tkcanvas.bind(名称、self.button\u释放\u事件)
对于“”中的名称:
self.\u tkcanvas.bind(名称、self.scroll\u事件)
root=self.\u tkcanvas.winfo\u toplevel()
root.bind(“,self.scroll\u事件\u窗口)
自我。_master=master
self.\u tkcanvas.focus\u set()
self.sourceed=dict()
def on_idle(*忽略):
self.idle_事件()
返回真值
def调整大小(自身、事件):
宽度,高度=event.width,event.height
printse(“WH”,宽度,高度,“\n”)
如果self.\u resize\u回调不是None:
自我调整大小回调(事件)
#以英寸为单位计算所需的图形大小
dpival=self.figure.dpi
绞车=宽度/dpival
hinch=高度/dpival
自定尺寸(绞车、铰链)
self.\u tkcanvas.delete(self.\u tkphoto)
self.\u tkphoto=tk.PhotoImage(
主控=自身。\ tkcanvas,宽度=宽度,高度=高度)
self.\u tkcanvas.创建图像(宽度/2,高度/2,图像=self.\u tkphoto)
self.resize_事件()
self.show()
哦,非常感谢它的有效性!!!诚恳地说,我觉得很奇怪,有必要进入库并修改它们,但它工作得非常好:)
# 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()