图像到Tkinter窗口

时间:2017-07-06 03:01:21

标签: python tkinter

我正在尝试将图像放入我的tkinter窗口并得到错误,不确定是什么原因造成的。该路径是我计算机上100%的位置。

import tkinter

import os

from PIL import ImageTk, Image


#Actual window        
window = tkinter.Tk()
window.geometry('400x400')
window.title('Game')
window.configure(bg = 'Indian red')

#Pictures
path = 'E:\Hack Things\python\amazonlogo'
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(window, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

#Buttons
start = tkinter.Button(text = 'Start', height = 5, width = 25, font =
('Helvetica', 12))

start.place(relx = .5, rely = .25, anchor='c')

options = tkinter.Button(text="Options")
options.place(relx = .5, rely = .45, anchor='c')

exitbutton = tkinter.Button(text = 'Exit Game', command = quit, height = 5, 
width = 15, fg = 'red')
exitbutton.place(relx = .5, rely = .85, anchor='c')

window.mainloop()

我收到此错误

Traceback (most recent call last):
  File "E:\Hack Things\python\start.py", line 16, in <module>
    img = ImageTk.PhotoImage(Image.open(path))
  File "C:\Users\Ihas\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\PIL\Image.py", line 2477, in open
    fp = builtins.open(filename, "rb")
OSError: [Errno 22] Invalid argument: 'E:\\Hack Things\\python\x07mazonlogo'

1 个答案:

答案 0 :(得分:2)

您需要使用原始字符串(在引号前添加UNIQUEHASH UNIQUEID1 UNIQUEID2 123 10 20 789 30 400 )。或者逃避反斜杠(通过在每个r前面添加\。由于\a 具有特殊含义,即“ASCII Bell(BEL)”

\