Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使图像显示在tkinter.Canvas上?_Python_Tkinter_Tkinter Canvas - Fatal编程技术网

Python 如何使图像显示在tkinter.Canvas上?

Python 如何使图像显示在tkinter.Canvas上?,python,tkinter,tkinter-canvas,Python,Tkinter,Tkinter Canvas,我有一个“游戏”类,它必须创建一个带有图像的窗口,下面是我尝试过的代码,但是图像没有出现。那里的目录存在 并且不会产生错误 from tkinter import * class Game(): def __init__(self): self.tk = Tk() self.tk.title(' ') self.tk.resizable(0, 0) self.tk.wm_attributes("-topmo

我有一个“游戏”类,它必须创建一个带有图像的窗口,下面是我尝试过的代码,但是图像没有出现。那里的目录存在 并且不会产生错误

from tkinter import *
class Game():
    def __init__(self):
        self.tk = Tk()
        self.tk.title('          ')
        self.tk.resizable(0, 0)
        self.tk.wm_attributes("-topmost", 1)
        self.canvas = Canvas(self.tk, width=800, height=600)
        self.canvas.pack()
        self.tk.update()
        self.bg = \
        PhotoImage('C:\\Users\\iv4um\\ninjafight\\hj\\officelight.gif')
        self.bgimage = self.canvas.create_image(0, 0, image=self.bg, \
        anchor='nw')
        self.tk.update()

我希望图像出现在屏幕上,但我得到的只是空白屏幕,没有图像。图像为800*600像素,为.gif。

如果您想使用Tkinter模块,您可以查看。不过,答案使用标签来显示gif

我建议你可以试试这个模块,它更简单

import pyglet

# giphy directory
giphy_file = "giphy.gif"

# make an animation object
animation = pyglet.resource.animation(giphy_file)

sprite = pyglet.sprite.Sprite(animation)

# create a window
win = pyglet.window.Window(width=sprite.width, height=sprite.height)

# background color = r, g, b, alpha of the window
green = 0, 1, 0, 1
pyglet.gl.glClearColor(*green)

@win.event
def on_draw():
     win.clear()
     sprite.draw()
pyglet.app.run()

尝试使用枕头模块(PIL,python图像库),您应该使用它将GIF作为图像处理

from PIL import Image, ImageTk

你查过了吗?根据您的问题,接受答案中的两个链接都会有所帮助。请尝试
self.bg=PhotoImage(file='C:\\Users\\iv4um\\ninjafight\\hj\\officelight.gif')
这比answers@AndriiChumakov谢谢对于一个正式的答案,我觉得不够复杂。问题是关于tkinter.Canvas的,而不是关于tkinter.Label或pyglet的。@AndriiChumakov是的。我有别的建议。我帮不了你。