如何使用Tkinter在python脚本中存储图像?

时间:2014-12-09 23:23:50

标签: python image tkinter

我想在tkinter中渲染一个图像但是我总是最终得到一个错误,即即使图像与python脚本(startupgui.py)在同一目录中,也无法找到图像(PieTalk.gif) :

 /Home/COMP3203/COMP30203-project/src/ptgui/

这是我想在GUI中渲染图像的方法。以下代码是一个名为startupgui.py的类。它由构造函数和加载图像的方法组成

构造

def __init__(self):

    # String to decide whether to go to client or server
    self.trigger = None

    #-----------------------#
    # Creating window frame #
    #-----------------------#
    self.root = Tk()
    self.root.wm_title("PieTalk")
    self.root.geometry('600x450')

    # creating PieTalk image
    self.createimage()
    # creating buttons
    self.createbuttons()

加载图片的方法:

def createimage(self):

    # Creating image frame
    self.imageframe = Frame(self.root, bg='light grey')
    self.imageframe.place(relx=0.1, relwidth=0.8, rely=0.05, relheight=0.65)

    # Creating PieTalk image
    self.pietalkimage=PhotoImage(file="PieTalk.gif")

    # Creating label
    self.imagelabel = Label(self.imageframe, image=pietalkimage)
    self.imagelabel.image = self.pietalkimage
    self.imagelabel.pack()

我只使用了文件名:

self.pietalkimage=PhotoImage(file="PieTalk.gif")

我还使用了文件的绝对路径:

self.pietalkimage=PhotoImage(file="/Home/COMP3203/COMP30203-project/src/ptgui/PieTalk.gif")

不幸的是,我在执行脚本时仍然遇到同样的错误:

Traceback (most recent call last):
  File "pietalk.py", line 361, in <module>
    startview=sgui.startupgui()
  File "/home/archit/COMP3203/COMP3203-project/src/ptgui/startupgui.py", line 66, in __init__
self.createimage()
  File "/home/archit/COMP3203/COMP3203-project/src/ptgui/startupgui.py", line 33, in createimage
  self.pietalkimage=PhotoImage(file="/Home/COMP3203/COMP30203-project/src/ptgui/PieTalk.gif")
  File "/usr/lib/python3.4/tkinter/__init__.py", line 3387, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 3343, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "/Home/COMP3203/COMP30203-project/src/ptgui/PieTalk.gif": no such file or directory

我在加载图片时还有其他问题吗?我还可以做什么来加载图像?

1 个答案:

答案 0 :(得分:2)

首先将其转换为base64 python变量

>>> import base64
>>> with open("my_image.py","w") as f:
...    f.write('my_image="""%s"""'%base64.b64encode(open("my_gif.gif","rb").read()))
...
>>> exit()

你现在应该有my_image.py文件...将它复制到与你的tkinter脚本相同的目录...现在你可以做到

from my_image import my_image
image=PhotoImage(data = my_image)

因为你遇到了一些问题,所以我们试着稍微简化一下

<强> img2base64.py

import base64,sys,os,re
assert len(sys.argv) > 2,"Error: Useage %s /path/to/image.gif outfile.py"
assert os.path.exists(sys.argv[1]),"Error Unable to find image passed in as first argument"
outfile = open(sys.argv[2],"w")
raw_binary_data = open(sys.argv[1],"rb").read()
b64_encoded_data = base64.b64encode(raw_binary_data)

varname = re.sub("[^W]","_",os.path.splitext(os.path.basename(sys.argv[1]))[0])
pyname = os.path.splitext(os.path.basename(sys.argv[2]))[0]
outfile.write("%s='''%s'''"%(varname,b64_encoded_data))
outfile.close()

print "all done please put %s in your script directory and use it as follows:"%sys.argv[2]
print "from %s import %s"%(pyname,varname)
print "image=PhotoImage(data = %s)"%(varname)

只需保存即可,然后将其调用

$ python img2base64.py /path/to/image.gif pyimage.py

我认为至少我没有尝试过......