Tkinter PhotoImage动画gif灰色斑点

时间:2017-03-30 05:53:39

标签: python animation tkinter gif

我已经很好地浏览了互联网和堆栈溢出,但我还没有找到答案。我已经创建了一个游戏作为我的一个课程评估的一部分,在这样做的过程中我决定我想要一个GIF动画作为背景图像并且没有问题。如果每个图像完全不同,动画将工作,它将迭代并正确显示图像。每个框架完全是不同的纯色。问题是,当它显示gif的帧时,不全新的点看起来是灰色的,所以只有"移动" gif的部分显示正确,其他一切都是灰色的?不幸的是,我无法附上图片来展示这个!如果可能的话,任何帮助将不胜感激。

class Game:
    def __init__(self, canvas, canvas_height, canvas_width, background_animation_speed):
        self.canvas = canvas
        self.canvas_height = canvas_height
        self.canvas_width = canvas_width
        self.background_animation_speed = background_animation_speed / 10  # converts from milliseconds to s/10
        self._background_iteration_period = self.background_animation_speed
        self.background_image_list = []
        self.background_image = Label(self.canvas, image="", borderwidth=0)
        self.sprite_list = []

    def load_assets(self):  # loads and stores images, can load any length gif.
        # allow this function to load images from multiple files / directories and store in different vars
        frames = []
        frame_counter = 1
        print("LOADING ASSETS")
        while True:
            try:
                frames.append(PhotoImage(file="images/giphy.gif", format="gif -index {}".format(frame_counter)))
                frame_counter += 1
            except TclError:
                # frame_counter = 1 not currently required
                break
        self.background_image_list = frames

    def display_game_screen(self):
        self.background_image.place(x=(self.canvas_width / 2) - 400, y=self.canvas_height - 600)

    def change_background_image(self):
        current_image = str(self.background_image.cget("image"))
        iteration_counter = 0
        for i in range(len(self.background_image_list)):
            iteration_counter += 1
            if current_image == str(self.background_image_list[i]) and self._background_iteration_period == \
                    self.background_animation_speed:
                if (i + 1) == len(self.background_image_list):
                    self.background_image.config(image=self.background_image_list[0])
                else:
                    self.background_image.config(image=self.background_image_list[(i + 1)])
                self._background_iteration_period = 0
            elif current_image != str(self.background_image_list[i]) and self._background_iteration_period == \
                    self.background_animation_speed and iteration_counter == len(self.background_image_list):
                self.background_image.config(image=self.background_image_list[0])
                self._background_iteration_period = 0
        self._background_iteration_period += 1

0 个答案:

没有答案