1

我有一个关于在类函数中使用 Python 结节的问题。

这是针对我正在尝试创建的 tkinter 应用程序。当用户加载一个文本文件时,会读入一个文本文件并用于创建一个 NetworkX 图形。当我在类中调用读入函数时,它无法访问 NetworkX 函数。

导致问题的代码行self.graph位于类内的图形对象下方,add_node是我要调用的 NetworkX 函数。

def __init__ (self, master = None, g = nx.graph, v = 'Some Number')
    #Call varibales
    self.graph = g
    self.value = v

def add_node():
    self.graph.add_node(self.value)

谢谢

4

1 回答 1

1

Hm.. the way your code is currently written, you're assigning the class itself (nx.graph) to g.

That, is a bit wrong, you'll need to instantiate nx.graph, so g is an instance of it:

def __init__ (self, master = None, g = nx.graph(), v = 'Some Number')

This way, g will be an instance of nx.graph when you make instantiate your class.

Alternatively, you can instantiate it when you assigned it to self.graph:

def __init__ (self, master = None, g = nx.graph, v = 'Some Number')
    #Call varibales
    self.graph = g()
    self.value = v

Hope this helps!

于 2013-11-01T04:14:46.430 回答