【问题标题】:imported Python module not initializing internal imports导入的 Python 模块未初始化内部导入
【发布时间】:2017-03-14 18:53:55
【问题描述】:

我有一个 Python / Pygame 模块,其结构如下所示。这被保存为一个名为 text_factory.py 的 python 文件。

import pygame, sys, os
from pygame.locals import KEYDOWN, K_ESCAPE, RLEACCEL

class Text_Factory(pygame.sprite.Sprite):
    def __init__(self, posx = None, posy = None, font = None, fontsize = 100, colour = (255, 50, 64), text1 = "Level ", text2 = " Complete!",  level = 1, frames = 30, pause = 1500, slowdown = 30, fade = False, rotate = None):

        pygame.sprite.Sprite.__init__(self)

        self.screen = pygame.display.get_surface()

        if self.screen == None:

            if sys.platform == 'win32' or sys.platform == 'win64':
                #os.environ['SDL_VIDEO_CENTERED'] = '0'# center of screen
                os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (10, 30)#top left corner

            self.screen =pygame.display.set_mode((800, 600), 1, 32)  # demo screen size

            back = pygame.image.load("D:\\IMAGES\\space_11.jpg").convert()
            self.screen.blit(back, (0, 0))

        # the subsurface is a rect
        self.subsurface = None  #self.screen.subsurface(0, 0, self.screen.get_width(), self.screen.get_height()).convert_alpha()
        self.background = (0, 0, 0)
        self.alpha = 255
        if posx == None:
            self.posx = self.screen.get_width() / 2
        else:
            self.posx = posx
        if posy == None:
            self.posy = self.screen.get_height() / 2
        else:
            self.posy = posy
        self.center = (self.posx, self.posy)

等等。等等

当我从下面的另一个 python 文件中导入它时

from text_factory import *

tf = Text_Factory()
tf.default() # this is a function in the class to demnstrate the code

程序停止运行并显示 pygame.error: font not initialized 消息。

显然 pygame 模块没有加载和初始化。 pygame、sys、os 和其他导入不应该在调用代码时运行并按照上面显示的方式运行吗?当我导入作为 python 模块的 text_factory 时,它不应该运行内部导入吗?如果没有怎么做?

【问题讨论】:

    标签: python-2.7 pygame


    【解决方案1】:

    我的错! 我根本没有在课堂上完成pygame.init()。一旦我插入,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-03
      • 2020-08-09
      • 2021-07-07
      • 2021-09-17
      • 1970-01-01
      • 2020-11-21
      • 2019-11-22
      • 2023-04-08
      相关资源
      最近更新 更多