Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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 打开图片_Python_Canvas_Tkinter - Fatal编程技术网

Python 打开图片

Python 打开图片,python,canvas,tkinter,Python,Canvas,Tkinter,我试着用下面的代码打开一张照片,但没有成功,它说文件不存在,它都在同一个文件夹中,我四处看了看,但似乎没有任何效果 from tkinter import * import sys from random import * def button1(): novi = Toplevel() canvas = Canvas ( novi , width = 300, height = 200 ) canvas.pack(expand = YES, fill = BOT

我试着用下面的代码打开一张照片,但没有成功,它说文件不存在,它都在同一个文件夹中,我四处看了看,但似乎没有任何效果

from tkinter import * 
import sys  
from random import *

def button1():
    novi = Toplevel()
    canvas = Canvas ( novi , width = 300, height = 200 )
    canvas.pack(expand = YES, fill = BOTH)
    gif1 = PhotoImage (file = 'hey')


    canvas.create_image (50, 10, image = gif1, anchor = NW)
    cancas.gif1 = gift1
mGui = Tk()
button1 = Button(mGui, text = 'hey boiz',command = button1, height = 5 , width = 20).pack()
mGui.mainloop()

您忘记了文件类型是
png
还是
jpg

gif1=PhotoImage(file='hey')
将其更改为
gif1=PhotoImage(file='hey.gif')
不要忘记文件类型

gif1 = PhotoImage(file='hey.png')
编辑:


更正了您的输入错误和我半睡半醒的回答。

您可能需要将文件扩展名添加到
file=
参数中。默认情况下,某些操作系统会对您隐藏此信息。例如,在WIndows上有一个文件夹选项,用于“
隐藏已知文件类型的扩展名”
”。看看你是否可以禁用它,这样你就可以看到它们了。如果文件是gif文件,不是更符合逻辑吗?由于变量名为
gif1
和。谢谢。我重新调整了我的代码,现在一切正常。谢谢你可以勾选它作为答案,以供将来参考coders@ConnorBurton你有几个打字错误。cancas.gif1=gift1应该是canvas.gif1=gif1。另外,file='hey.gif'它在我的屏幕上工作。
gif1 = PhotoImage(file='hey.gif')
canvas.create_image (50, 10, image = gif1, anchor = NW)
canvas.gif1 = gif1