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