[英]How to call a method in python that is in a different class, where the class is in a different file?
我有一个名为main.py
的文件,其中包含一个名为mainWindow
的类。 我还有另一个名为popupWindow.py
文件,其中包含一个名为popupWindow
的类。 mainWindow
类包含2个方法。 一个名为clearListBox
将清除主窗口中的列表框,另一个名为addScouts(I)
的递归函数用于将存储在文件中的侦察器写入列表框。 我希望能够从我的类popupWindow
调用clearListBox
和addScouts(I)
。 我该如何实现?
在尝试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'
先感谢您
这个问题已经被问过一次又一次-与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)
或实例化时间:
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
,它只需要获取一个实例作为参数即可。 因此,如果A
和B
位于不同的模块中,则包含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()
这避免了您显然已经回溯的循环导入错误。
在popupWindow.py
的开头, popupWindow.py
一行
from main import mainWindow
然后您可以调用,例如mainWindow.clearListBox()
在OP发布代码示例之后进行编辑: clearListBox
是一个实例方法,因此只能在实例上调用,而不能在类本身上调用。 首先,您必须实例化mainWindow
类型的对象。
根据您的问题和对@niceguy答案的评论,很明显,解决问题的方法是阅读python教程 :很快您将了解模块(包括import
)和类(包括self
和调用类)方法)。
编辑:如果您已经了解类和实例,那么这就是您的问题了:类名是mainWindow
,实例是mainWin
。 您应该在mainWin
上调用函数,例如mainWin.addScouts(1)
; 不在mainWindow
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.