我无法使用画布和create_image在tkin中打开图像

2024-04-20 14:14:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我在为观众做游戏复制游戏果酱。我试图把一个图像放在一个画布上,这个画布是在一个不同的类中创建的,而不是创建图像的类。画布已经成功地显示了文本和带有我需要的图像的按钮,但是create_image不起作用。你知道吗

这可能是一个分辨率的问题,但我现在不能检查,图像是1920x1080,所以是游戏。你知道吗

我已经试过了。你知道吗

Full program(包括图像)

class game(Application):
    """Where game elements are to be held"""

    def __init__(self, players, location, sWidth, sHeight, canvas):
        """Initiate variables & start game"""

        #Initiate variables
        self.players = players
        self.location = location
        self.screenWidth = sWidth
        self.screenHeight = sHeight
        self.Canvas1 = canvas

        self.LOCATIONS = Application.LOCATIONS
        self.font = Application.font

        #Gathering Images
        self.map1BG = PhotoImage(file = "polasib.gif")

        #Debugging
        print("Loading Map", self.location\
        , "\nPlayers:", self.players)

        self.createLevel(self.location)

    def createUI(self, players):
        """Creates the UI that all levels will have"""
        self.Canvas1.create_text(self.screenWidth/2, self.screenHeight/16, fill = "white", \
        font = (self.font, self.screenWidth//34), text = self.LOCATIONS[self.location - 1])

    def createLevel(self, location):
        """Creates the elements of the level"""


        if self.location == 1:
            #Polasi b
            print("Creating Polasi b Level")
            self.createUI(self.players) 

            self.Canvas1.create_image(self.screenWidth/2, self.screenHeight/2, image = self.map1BG, \
            anchor = NW)

期望:我期望加载映像(并且它需要一些重新调整)

结果:没有图像出现,但其他添加的内容(作为测试)都正常。你知道吗


Tags: 图像imageselfgame游戏applicationdef画布
1条回答
网友
1楼 · 发布于 2024-04-20 14:14:39

由于您没有保存对game实例的引用,因此它将在退出Application.gameScreen()后被销毁。因此create_imageimage的引用将丢失并且不显示图像。尝试将game的实例赋给Application的实例变量,如下所示:

self.running_game = game(self.players, self.mapChosen, self.screenWidth, self.screenHeight, self.Canvas1)

相关问题 更多 >