问题描述
我试图在Ubuntu和Python 2.7上使用Nodebox Graph。
所以我得到了Nodebox OpenGL:
Nodebox图表:
我试图运行他们的基本示例1:
graph = ximport(graph)
size(500,500 )
g = graph.create()
#创建一些关系。
g.add_edge(roof,house)
g.add_edge(garden,house)
g.add_edge(room,house)
g.add_edge(厨房,房间)
g.add_edge(卧室,房间)
g.add_edge(浴室,房间)
g.add_edge(客厅,房间)
g.add_edge(沙发,客厅)
g.add_edge(餐桌,客厅)
#计算一个好的布局。
g.solve()
#为节点颜色和大小应用默认规则,
#例如,重要节点变为蓝色。
g.styles.apply()
#绘制图表,指出每个关系
#的方向以及获得最多流量的两个节点。
g.draw(
directed = True,
traffic = 1
)
这是行不通的,因为ximport没有定义,它只是由nodebox定义的,所以我试了两件事,第一次做一个正常导入
导入图
第二次放置在我的代码中从nodebox的ximport函数:
def ximport(库):$ b $ from sys import modules
library = __import __(library)
library._ctx = modules [__ name__]
返回库
graph = ximport(graph)
size(500,500)
g = graph.create()
#创建一些关系。
g.add_edge(roof,house)
g.add_edge(garden,house)
g.add_edge(room,house)
g.add_edge(厨房,房间)
g.add_edge(卧室,房间)
g.add_edge(浴室,房间)
g.add_edge(客厅,房间)
g.add_edge(沙发,客厅)
g.add_edge(餐桌,客厅)
#计算一个好的布局。
g.solve()
#为节点颜色和大小应用默认规则,
#例如,重要节点变为蓝色。
g.styles.apply()
#绘制图表,指出每个关系
#的方向以及获得最多流量的两个节点。
g.draw(
directed = True,
traffic = 1
)
这仍然不起作用,因为现在函数大小无法识别。如果我只是评论大小,我得到以下错误:
我该怎么做?
这个问题可能类似:
但给定的答案对我来说没有任何帮助。
/code/index.php/Graphrel =nofollow> https://www.nodebox.net/code/index.php/Graph不适用于nodebox opengl,它是只能与节点盒1 for mac兼容,所以没有简单的解决方法。但是,nodebox opengl拥有自己的交互功能,因此可以使用它们。以下是部分示例代码:
from nodebox.graphics import *
from nodebox.graphics.physics import Node,Edge ,图
类图形用户界面():
绘制并与该死的东西进行交互
def __init __(self):
rawgraph是RawGraph类的一个实例
GUI允许与它进行交互,即查看它并将其更改。
#Layer .__ init __(self)#未知的事情,可能是必要的。
#Buttons
self.EXPLORE_F =进一步探索
self.EXPLORE_D =深入探索
self.DELETE =删除
self.CLOSE =关闭
self.BUTTONS = False#是否显示按钮?
self.CBUTTON = False#是否显示关闭按钮?
self.nodes = []
self.g =图表()
self.dragged =无
self.clicked = None
def add_node(self,name,root = False):
self.nodes.append(name)
self .g.add_node(id = name,radius = 5,root = root)
$ b $ def add_edge(self,n1,n2,* args,** kwargs):
self.g.add_edge(n1,n2,* args,** kwargs)
def explore_further(self,node_name):
探索更多按钮的动作
通过
def explore_deeper(self,node_name):
探索更深层按钮的动作
通过
def delete_node(self,node_name):
删除按钮的操作
通过
def close_canvas(self):
关闭画布
canvas.clear()
canvas.stop()
$ b $ def add_close(self):
添加关闭按钮
self.g.add_node(self.CLOSE,radius = 10,fill =(1,0,0,0.2 ))
self.g.add_edge(self.rawg.root,self.CLOSE,length = 0.6)
self.CBUTTON = True
def remove_close(self):
删除关闭按钮
尝试:
self.g.remove(self.g.node(self.CLOSE))
除外:
传递
self.CBUTTON = False
def add_buttons(self,node_name):
添加按钮以更改图形
$self.g.add_node(self.EXPLORE_F,radius = 6,fill =(0,1,0,0.5))
self.g.add_node(self.EXPLORE_D,radius = 6,fill =(0,0,1,0.5))
self.g.add_node(self.DELETE,radius = 6,fill =(1,0,0,0.5))
self .g.add_edge(node_name,self.EXPLORE_F,length = 0.2)
self.g.add_edge(node_name,self.EXPLORE_D,length = 0.2)
self.g.add_edge(node_name,self.DELETE ,长度= 0.2)
self.BUTTONS = True
def remove_buttons(self):
移除按钮以更改图形
试试:
self.g.remove(self.g.node(s elf.DELETE))
self.g.remove(self.g.node(self.EXPLORE_D))
self.g.remove(self.g.node(self.EXPLORE_F))
除外:
传递
self.BUTTONS = False
def draw(self):
canvas.clear()
background(1)
translate(500,500)
#如果指示=真,边线会有箭头指示连接的方向。
#With weighted = True,Node.centrality在高流量节点下由阴影表示。
#加权= 0.0-1.0,表示节点的中心性>给定的阈值。
#这需要一些额外的计算。
self.g.draw(加权= 0.5,定向=假)
self.g.update(迭代= 10)
#让它互动!
#按下鼠标时,请记住在哪个节点上。
#鼠标移动时,拖动此节点。
dx = canvas.mouse.x - 500#撤消translate()。
dy = canvas.mouse.y - 500
#global拖动
如果canvas.mouse.pressed而不是self.dragged:
self.dragged = self.g.node_at( dx,dy)
old_clicked = self.clicked
try:
self.clicked = self.dragged.id
除外:
self.clicked = None
如果self.clicked!=无:
如果self.clicked == self.DELETE:
如果old_clicked!=无:
self.delete_node(old_clicked )
self.remove_buttons()
elif self.clicked == self.EXPLORE_D:
如果old_clicked!=无:
self.explore_deeper(old_clicked)
self。 remove_buttons()
elif self.clicked == self.EXPLORE_F:
if old_clicked!= None:
self.explore_further(old_clicked)
self.remove_buttons()
elif self.clicked == self.CLOSE:
self.remove_buttons()
self.remove_close()
self.close_canvas()
else:
self.remove_buttons ()
self.remove_close()
self.add_buttons(self.clicked)
else:
如果self.BUTTONS:
self.remove_buttons( )
elif self.CBUTTON:
self.remove_close()
else:
self.remove_buttons()
self.add_close()
如果不是canvas.mouse.pressed:
self.dragged =无
如果self.dragged:
self.dragged.x = dx
self.dragged.y = dy
def start(self,distance = 30,force = 0.01,repulsion_radius = 30):
启动GUI
#self.g.prune(depth = 0)#删除没有连接的孤立节点。
self.g.distance =距离#节点之间的总体间距。
self.g.layout.force = force#吸引力&排斥力。
self.g.layout.repulsion = repulsion_radius#排斥半径。
canvas.draw = self.draw
canvas.size = 1000,1000
canvas.run()
if __name__ =='__main__':
gui = GUI()
gui.add_node(a)
gui.add_node(b)
gui.add_edge(a,b)
gui.start(距离= 30,repulsion_radius = 30)
I am trying to use Nodebox Graph on Ubuntu and python 2.7.
So I got Nodebox OpenGL: http://www.cityinabottle.org/nodebox/
Nodebox Graph: https://www.nodebox.net/code/index.php/Graph
I tried to run their basic example 1 :
graph = ximport("graph")
size(500, 500)
g = graph.create()
# Create some relations.
g.add_edge("roof" , "house")
g.add_edge("garden" , "house")
g.add_edge("room" , "house")
g.add_edge("kitchen" , "room")
g.add_edge("bedroom" , "room")
g.add_edge("bathroom" , "room")
g.add_edge("living room" , "room")
g.add_edge("sofa" , "living room")
g.add_edge("table" , "living room")
# Calculate a good layout.
g.solve()
# Apply default rules for node colors and size,
# for example, important nodes become blue.
g.styles.apply()
# Draw the graph, indicating the direction of each relation
# and the two nodes that get the most traffic.
g.draw(
directed=True,
traffic=1
)
That doesn't work because ximport is not defined, it is only define by nodebox, so instead I tried two things, first doing a normal import import graphsecond putting the ximport function from nodebox in my code:
def ximport(library):
from sys import modules
library = __import__(library)
library._ctx = modules[__name__]
return library
graph = ximport("graph")
size(500, 500)
g = graph.create()
# Create some relations.
g.add_edge("roof" , "house")
g.add_edge("garden" , "house")
g.add_edge("room" , "house")
g.add_edge("kitchen" , "room")
g.add_edge("bedroom" , "room")
g.add_edge("bathroom" , "room")
g.add_edge("living room" , "room")
g.add_edge("sofa" , "living room")
g.add_edge("table" , "living room")
# Calculate a good layout.
g.solve()
# Apply default rules for node colors and size,
# for example, important nodes become blue.
g.styles.apply()
# Draw the graph, indicating the direction of each relation
# and the two nodes that get the most traffic.
g.draw(
directed=True,
traffic=1
)
That still doesn't work because now the function size is not recognized. If I just comment size out, I get the following error:
What do I do?
This question could be similar:
Pydev Nodebox: "AttributeError: 'NoneType' object has no attribute 'WIDTH'"
but the given answer is not helpful at all to me.
The code in https://www.nodebox.net/code/index.php/Graphdoesn't work for nodebox opengl, it is only compatible for nodebox 1 for mac so there is no easy fix. However, nodebox opengl has it's own interactive functions to it is possible to use them instead. Here is partial example code:
from nodebox.graphics import *
from nodebox.graphics.physics import Node, Edge, Graph
class GUI():
"""Draw and interact with the damn thing.
"""
def __init__(self):
"""rawgraph is an instance of the RawGraph class
The GUI allows to interact with it, i.e. view it and change it.
"""
#Layer.__init__(self) # unknown thing, might be necessary.
#Buttons
self.EXPLORE_F = "Explore Further"
self.EXPLORE_D = "Explore Deeper"
self.DELETE = "Delete"
self.CLOSE = "CLOSE"
self.BUTTONS = False #Are buttons showing?
self.CBUTTON = False #Is Close button showing?
self.nodes = []
self.g = Graph()
self.dragged =None
self.clicked = None
def add_node(self, name, root = False):
self.nodes.append(name)
self.g.add_node(id=name, radius = 5, root = root)
def add_edge(self,n1,n2,*args,**kwargs):
self.g.add_edge(n1, n2, *args,**kwargs)
def explore_further(self,node_name):
"""action of explore further button
"""
pass
def explore_deeper(self,node_name):
"""action of explore deeper button
"""
pass
def delete_node(self,node_name):
"""action of delete button
"""
pass
def close_canvas(self):
"""Close the canvas
"""
canvas.clear()
canvas.stop()
def add_close(self):
"""Add close button
"""
self.g.add_node(self.CLOSE, radius = 10, fill=(1,0,0,0.2))
self.g.add_edge(self.rawg.root, self.CLOSE, length=0.6)
self.CBUTTON=True
def remove_close(self):
"""Remove the close button
"""
try:
self.g.remove(self.g.node(self.CLOSE))
except:
pass
self.CBUTTON=False
def add_buttons(self,node_name):
"""Add the buttons to change the graph
"""
self.g.add_node(self.EXPLORE_F, radius = 6, fill=(0,1,0,0.5))
self.g.add_node(self.EXPLORE_D, radius = 6, fill=(0,0,1,0.5))
self.g.add_node(self.DELETE,radius=6, fill=(1,0,0,0.5))
self.g.add_edge(node_name, self.EXPLORE_F, length=0.2)
self.g.add_edge(node_name, self.EXPLORE_D, length=0.2)
self.g.add_edge(node_name, self.DELETE, length=0.2)
self.BUTTONS = True
def remove_buttons(self):
"""Remove the buttons to change the graph
"""
try:
self.g.remove(self.g.node(self.DELETE))
self.g.remove(self.g.node(self.EXPLORE_D))
self.g.remove(self.g.node(self.EXPLORE_F))
except:
pass
self.BUTTONS=False
def draw(self):
canvas.clear()
background(1)
translate(500, 500)
# With directed=True, edges have an arrowhead indicating the direction of the connection.
# With weighted=True, Node.centrality is indicated by a shadow under high-traffic nodes.
# With weighted=0.0-1.0, indicates nodes whose centrality > the given threshold.
# This requires some extra calculations.
self.g.draw(weighted=0.5, directed=False)
self.g.update(iterations=10)
# Make it interactive!
# When the mouse is pressed, remember on which node.
# Drag this node around when the mouse is moved.
dx = canvas.mouse.x - 500 # Undo translate().
dy = canvas.mouse.y - 500
#global dragged
if canvas.mouse.pressed and not self.dragged:
self.dragged = self.g.node_at(dx, dy)
old_clicked = self.clicked
try:
self.clicked = self.dragged.id
except:
self.clicked = None
if self.clicked != None:
if self.clicked == self.DELETE:
if old_clicked != None:
self.delete_node(old_clicked)
self.remove_buttons()
elif self.clicked == self.EXPLORE_D:
if old_clicked != None:
self.explore_deeper(old_clicked)
self.remove_buttons()
elif self.clicked == self.EXPLORE_F:
if old_clicked != None:
self.explore_further(old_clicked)
self.remove_buttons()
elif self.clicked == self.CLOSE:
self.remove_buttons()
self.remove_close()
self.close_canvas()
else:
self.remove_buttons()
self.remove_close()
self.add_buttons(self.clicked)
else:
if self.BUTTONS:
self.remove_buttons()
elif self.CBUTTON:
self.remove_close()
else:
self.remove_buttons()
self.add_close()
if not canvas.mouse.pressed:
self.dragged = None
if self.dragged:
self.dragged.x = dx
self.dragged.y = dy
def start(self, distance = 30, force = 0.01, repulsion_radius=30):
"""Starts the GUI
"""
#self.g.prune(depth=0) # Remove orphaned nodes with no connections.
self.g.distance = distance # Overall spacing between nodes.
self.g.layout.force = force # Strength of the attractive & repulsive force.
self.g.layout.repulsion = repulsion_radius # Repulsion radius.
canvas.draw = self.draw
canvas.size = 1000, 1000
canvas.run()
if __name__ == '__main__':
gui = GUI()
gui.add_node("a")
gui.add_node("b")
gui.add_edge("a","b")
gui.start(distance=30, repulsion_radius=30)
这篇关于Nodebox打开GL Graph,尺寸功能无法识别。 (Ubuntu的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!