【问题标题】:no such file or directory python image [duplicate]没有这样的文件或目录python图像[重复]
【发布时间】:2018-03-14 18:41:19
【问题描述】:

我正在尝试将图像放在 tkinter 按钮上,但它似乎不起作用。

root.geometry('512x512')
vbuckimage = tkinter.PhotoImage(file='vbuckicon.gif')
#vbucks button
vbucksButton = tkinter.Button(root,image=vbuckimage,height=5,width=10)

vbucksButton.place(x=256,y=256)
root.mainloop()

图像与 .py 文件位于同一位置

【问题讨论】:

  • 它正在寻找相对于您当前工作目录的文件,该文件可能与脚本的位置不同。

标签: python python-2.7 tkinter


【解决方案1】:

你似乎和here有同样的问题 试试这个解决方案:

import os
from tkinter import *
base_folder = os.path.dirname(__file__)
image_path = os.path.join(base_folder, 'vbuckicon.gif')
vbuckimage = PhotoImage(file=image_path)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2016-08-11
    • 2014-06-18
    • 2019-12-23
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多