如何在不同的类中调用python中的方法,其中的类位于不同的文件中?

问题描述:

我有一个名为main.py的文件,其中包含一个名为mainWindow的类。我有第二个文件名为popupWindow.py,其中包含一个名为popupWindow的类。 mainWindow类包含2种方法。一个名为clearListBox,它清除主窗口中的列表框,另一个名为addScouts(I),它是递归函数,用于将存储在文件中的侦察队列写入列表框。我希望能够从我的课popupWindow拨打clearListBoxaddScouts(I)。我如何实现这一目标?如何在不同的类中调用python中的方法,其中的类位于不同的文件中?

在试图from main import mainWindow然后调用mainWindow.addScouts(1)我收到addScouts要求误差ARG self

在我main.py文件:

class mainWindow: 
    def __init__(self,master): 
     self.master = master 
     self._scouts = [] 
     addBtn = Button(master,text="Create Scout",command=self._createScout) 
     addBtn.pack() 
     remBtn = Button(master,text="Remove Scout",command=self._removeScout) 
     remBtn.pack() 
     fndBtn = Button(master,text="Find Scout",command=self._findScout) 
     fndBtn.pack() 
     exitBtn = Button(master,text="Exit",command=self._exit) 
     exitBtn.pack() 
     scoutList = Listbox(master) 
     scoutList.pack() 
     self.scoutList = scoutList 
     self.addScouts(1) 
     w = 1000 #The value of the width 
     h = 750 #The value of the height of the window 

     # get screen width and height 
     ws = root.winfo_screenwidth()#This value is the width of the screen 
     hs = root.winfo_screenheight()#This is the height of the screen 

     # calculate position x, y 
     x = (ws/2) - (w/2) 
     y = (hs/2) - (h/2) 

     #This is responsible for setting the dimensions of the screen and where it is 
     #placed 
     root.geometry('%dx%d+%d+%d' % (w, h, x, y)) 
     self._createLeaderboard() 

    def addScouts(self,I): 
     i = I 
     with open(fileName,"r") as f: 
      lines = f.readlines() 
      for line in lines: 
       if str(line.split(",")[3])[:-1] == str(i): 
        self.scoutList.insert(END,line[:-1]) 
        i += 1 
        return self.addScouts(i) 
     return 

    def clearListBox(self): 
     self.scoutList.delete(0,END) 
     return 

popupWindow.py

from main import mainWindow 

popupWindow类:

mainWindow.clearListBox() 
mainWindow.addScouts(1) 

我的错误:

Traceback (most recent call last): 
    File "C:\Users\KRIS\Documents\Python Projects\Scouts\popupWindow.py", line  4, in <module> 
    from main import mainWindow 
    File "C:\Users\KRIS\Documents\Python Projects\Scouts\main.py", line 4, in  <module> 
    from popupWindow import * 
    File "C:\Users\KRIS\Documents\Python Projects\Scouts\popupWindow.py", line 4, in <module> 
    from main import mainWindow 
ImportError: cannot import name 'mainWindow' 

预先感谢您

+0

您可以通过使用['import'](https://docs.python.org/3/reference/import.html)模式导入所需的类来完成此操作。尝试使用子模块语句所示的['import-from'](https://docs.python.org/3/reference/import.html#submodules)导入包含所需方法的类。但是,你应该考虑一下你的文件。目录结构关于主和辅助文件,因为它似乎你想要将主要方法导入到帮助文件。 – albert

+0

'import'是该语言的关键字,而不是“模式” – alexis

+0

发布代码@AdminHydra –

这个问题已被问及次数和次数 - 并没有什么特定于Python的。要调用另一个类的实例的实例,则需要引用此实例。非常明显的解决方案是通过该基准,无论是在通话时间:

class A(object): 
    def __init__(self, var_a): 
     self.var_a = var_a 

    def method(self, another_object): 
     return another_object.another_method(self.var_a) 


class B(object): 
    def __init__(self, var_b): 
     self.var_b = var_b 

    def another_method(self, var): 
     return self.var_b + var 


a = A(42) 
b = B(1138) 
print a.method(b) 

或instanciation时间:

class A(object): 
    def __init__(self, var_a, another_object): 
     self.var_a = var_a 
     self.another_object = another_object 

    def method(self): 
     return self.another_object.another_method(self.var_a) 


class B(object): 
    def __init__(self, var_b): 
     self.var_b = var_b 

    def another_method(self, var): 
     return self.var_b + var 

b = b(1138) 
a = A(b) 
print a.method() 

注意,在这两种情况下,B并不需要了解A类 - 它只是得到一个实例作为参数,就是这样。所以,如果AB住在不同的模块,包含B模块没有导入包含A之一:

# module b.py 
class B(object): 
    def __init__(self, var_b): 
     self.var_b = var_b 

    def another_method(self, var): 
     return self.var_b + var 

# module a.py 

from b import B 

class A(object): 
    def __init__(self, var_a, another_object): 
     self.var_a = var_a 
     self.another_object = another_object 

    def method(self): 
     return self.another_object.another_method(self.var_a) 

if __name__ == "__main__":  
    b = b(1138) 
    a = A(b) 
    print a.method() 

避免了你显然已经给你回溯循环导入错误。

+0

这对我没有帮助,因为我的类存储在不同的文件中,这似乎是造成问题的原因 –

+0

@AdminHydra从另一个模块导入模块不应该成为某个声称了解Python的人的问题,应该这样做吗?我的意思是,它是教程的一部分... https://docs.python.org/3/tutorial/modules.html#the-module-search-path –

+0

我很抱歉不得不说你还需要了解上述 - 特别是类和实例之间的区别。 –

截至popupWindow.py年初,放线

from main import mainWindow 

然后,你可以调用,例如mainWindow.clearListBox()

在OP发布代码示例之后编辑: clearListBox是一种实例方法,因此只能在实例上调用,而不能在类本身上调用。您首先必须实例化mainWindow类型的对象。

+0

我试过这个,但是clearListBox需要self,因为它包含在mainWindow类中,并且引用了该类中包含的列表 –

+0

我想你需要在这里发布一个最简单的例子。 “自我”只有在课堂上才有意义,所以我不太明白你的意思。 – niceguy

+0

很显然,OP正在看一个复杂的程序,他们不知道足够的Python来理解它。不要试图在这个答案的空间中教他/她的Python。 – alexis

根据您的问题和您对@ niceguy的回答的评论,很明显,您的问题的解决方案是阅读the python tutorial:很快,您将了解模块(包括import)和课程(包括self和调用类方法)。

编辑:如果您已经了解关于类和实例,那么这里是你的问题阐述了:你的类名是mainWindow,您的实例是mainWin。您应该在mainWin上拨打您的功能,例如mainWin.addScouts(1);不在mainWindow