Python 模块无法与pygame一起正常工作
我无法使用主模块中其他模块中的函数Python 模块无法与pygame一起正常工作,python,pygame,Python,Pygame,我无法使用主模块中其他模块中的函数 #MAIN FILE - mainfile.py #Imports / display / pygame.init from file2 import * font=pygame.font.SysFont("Arial",35) example(1) #THERE I DEFINE DE CLASS Text=font.render(list1[0],0,(255,255,255)) win.blit(Text,(5,5))
#MAIN FILE - mainfile.py
#Imports / display / pygame.init
from file2 import *
font=pygame.font.SysFont("Arial",35)
example(1) #THERE I DEFINE DE CLASS
Text=font.render(list1[0],0,(255,255,255))
win.blit(Text,(5,5))
#while loop
我知道我可以在文件2中定义示例类,但我想在主文件中定义它。在您的示例中,
函数
可能比类
更好。最好使用return
,而不是global
在file2.py中
def get_list(whichlist)
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
from file2 import get_list
list1 = get_list(1)
class Example:
def get_list(self, whichlist):
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
from file2 import Example
ex = Example()
list1 = ex.get_list(1)
在file1.py中
def get_list(whichlist)
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
from file2 import get_list
list1 = get_list(1)
class Example:
def get_list(self, whichlist):
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
from file2 import Example
ex = Example()
list1 = ex.get_list(1)
若您真的需要使用类,那个么您应该在类中创建使用
return
对类使用CamelCaseNames
也有一个很好的规则——它的意思是Example
而不是'Example'
在file2.py中
def get_list(whichlist)
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
from file2 import get_list
list1 = get_list(1)
class Example:
def get_list(self, whichlist):
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
from file2 import Example
ex = Example()
list1 = ex.get_list(1)
在file1.py中
def get_list(whichlist)
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
from file2 import get_list
list1 = get_list(1)
class Example:
def get_list(self, whichlist):
if whichlist == 1:
return ["bird", "bird2", "bird3"]
elif whichlist == 2:
return ["bird", "bird2", "bird3"]
#else:
# return []
from file2 import Example
ex = Example()
list1 = ex.get_list(1)
最终
list1 = Example().get_list(1)
仅仅把它放在那里到底有什么问题?我如何在主文件中定义类而不产生错误,我正在讨论示例(1)您能否详细解释“错误”是什么?要在文件1中定义,您必须将代码从文件2复制到文件2。但可能您的意思是“如何使用来自其他文件的类”。但在您的示例中,
函数
似乎比类
更好。