【问题标题】:tkinter opening a picturetkinter 打开图片
【发布时间】:2018-11-28 10:54:00
【问题描述】:

我尝试使用以下代码打开照片,但没有成功,它说文件不存在,并且都在同一个文件夹中,我环顾四周,但似乎没有任何效果

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()

【问题讨论】:

  • 您可能需要将文件的扩展名添加到file= 参数。默认情况下,某些操作系统会对您隐藏这一点。例如,在 WINdows 上有一个“Hide extensions for known file types”的文件夹选项。看看你能不能禁用它,这样你才能看到它们。

标签: python canvas tkinter


【解决方案1】:

您忘记了文件类型是png 还是jpg gif1 = PhotoImage (file = 'hey')改成gif1 = PhotoImage (file = 'hey.gif')

【讨论】:

【解决方案2】:

不要忘记文件类型。

gif1 = PhotoImage(file='hey.png')

编辑:

gif1 = PhotoImage(file='hey.gif')
canvas.create_image (50, 10, image = gif1, anchor = NW)
canvas.gif1 = gif1

更正了你的错别字,以及我半睡半醒的回答。

【讨论】:

  • @ConnorBurton 你有几个错别字。 cancas.gif1 = gift1 应该是 canvas.gif1 = gif1。此外,file = 'hey.gif' 它适用于我的屏幕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 2019-12-13
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多