[英]I cannot open an image using a canvas and create_image on tkinter
我正在为repl.it游戏卡纸制作游戏。 我试图将图像放在与创建图像的类不同的类中创建的画布上。 画布已成功显示文本和带有所需图像的按钮,但create_image
无法正常工作。
这可能是分辨率的问题,但我现在无法检查,图像为1920 x 1080,游戏也是如此。
我已经尝试过create_image
。
完整程序 (包括图像)
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)
期望值:我希望图像能够加载(并且需要重新调整)
结果:没有图像出现,但是其他所有内容(作为测试)都可以使用。
由于您没有保存对game
实例的引用,因此退出Application.gameScreen()
后,该引用将被销毁。 因此, create_image
image
的参考将丢失,并且不会显示任何映像。 尝试将game
实例分配给Application
的实例变量,如下所示:
self.running_game = game(self.players, self.mapChosen, self.screenWidth, self.screenHeight, self.Canvas1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.