Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 模块无法与pygame一起正常工作_Python_Pygame - Fatal编程技术网

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。但可能您的意思是“如何使用来自其他文件的类”。但在您的示例中,
函数
似乎比
更好。