我无法在tkinter上使用canvas和create_image打开图像

问题描述 投票:0回答:1

我正在为repl.it游戏制作游戏。我试图将图像放在与创建图像的类不同的类中创建的画布上。画布已成功显示文本和带有我需要的图像的按钮,但create_image无效。

这可能是分辨率的问题,但我现在无法检查,图像是1920 x 1080,游戏也是如此。

我已经尝试过create_image了。

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)

期望:我希望图像加载(并且需要重新调整)

结果:没有图像出现但其他所有内容(作为测试)都有效。

python tkinter
1个回答
1
投票

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

self.running_game = game(self.players, self.mapChosen, self.screenWidth, self.screenHeight, self.Canvas1)
© www.soinside.com 2019 - 2024. All rights reserved.