在Windows上运行Graph库和nodebox + None类型错误

1 投票
1 回答
1093 浏览
提问于 2025-04-16 18:25

我在我的电脑上安装了nodebox 2,并且确认所有示例都能正常运行。

现在我想使用图形库Graph

我把它直接复制到了我的site-packages文件夹里,然后在IDLE中运行了它提供的示例。

结果我收到了一个关于ximport的错误。于是我在代码中添加了from nodebox.graphics import *

现在我遇到了以下错误:

Traceback (most recent call last):

File "C:\Python26\Lib\site-packages\graph\graph_example2.py", line 39, in <module> g.draw(highlight=path, weighted=True, directed=True)

File "C:\Python26\lib\site-packages\graph\__init__.py", line 453, in draw self.update()

File "C:\Python26\lib\site-packages\graph\__init__.py", line 416, in update self.x = _ctx.WIDTH - max.x*self.d - min_.x*self.d

AttributeError: 'NoneType' object has no attribute 'WIDTH'

有没有办法让我在Windows上从nodebox外部运行这个库呢?

谢谢...

我把导致错误的代码粘贴在下面...

from nodebox.graphics import *
try:
    graph = ximport("graph")
except ImportError:
    graph = ximport("__init__")
    reload(graph)

size(600, 600)

# A graph object.
g = graph.create(iterations=500, distance=1.0)

# Add nodes with a random id,
# connected to other random nodes.
for i in range(50):
    node1 = g.add_node(random(500))
    if random() > 0.5:
        for i in range(choice((2, 3))):
             node2 = choice(g.nodes)
             g.add_edge(node1.id, node2.id, weight=random())

# We leave out any orphaned nodes.
g.prune()

# Colorize nodes.
# Nodes with higher importance are blue.
g.styles.apply()

# Update the graph layout until it's done.
g.solve()

# Show the shortest path between two random nodes.
path = []
id1 = choice(g.keys())
id2 = choice(g.keys())
path = g.shortest_path(id1, id2)

# Draw the graph and display the shortest path.
g.draw(highlight=path, weighted=True, directed=True)

1 个回答

2

Nodebox Graph文档提到它支持Nodebox 1.9.5.6,这是一个仅限Mac的Nodebox 1版本。根据我所知,Graph库还没有移植到Nodebox 2,所以目前只能在Mac上运行。


一个选择是一个叫做Nodebox OpenGL的项目,它实现了Nodebox的API,并包含自己的图形库,里面有一个使用示例在examples\08-physics\07-graph目录下。Nodebox 1的Graph库还不兼容,但它有自己的图形类nodebox.graphics.physics.Graph

要使用它,你需要下载:

解压这些文件并安装,或者把nodeboxpyglet包放在你的Python路径中的某个地方(site-packages)。当你运行07-graph.py时,你应该能看到这个:

enter image description here

撰写回答