当我对图像使用地址时,显示图像的代码出现语法错误。
from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
Scare = Tk()
Scare.title('?????')
Countdown = 2
CountTotal = 2
CountTotal = IntVar()
def CountdownWork():
global Countdown
if Countdown > 0:
Countdown = Countdown -1
CountTotal.set(Countdown)
Scare.after(1000, CountdownWork)
else:
ImageAddress = 'C:\Users\KINSLED\Desktop\New folder\ScareTest.jpg'
ImageItself = Image.open(ImageAddress)
ImageNumpyFormat = np.asarray(ImageItself)
plt.imshow(ImageNumpyFormat)
plt.draw()
plt.pause(5) # pause how many seconds
plt.close()
Count = Label(Scare, font=('arial', 10, 'bold'), textvariable=CountTotal,
bd=30, bg='SeaGreen1', justify='right').grid(row=7,columnspan=8)
CountdownWork()
Scare.mainloop()
语法错误是突出显示 ImageAdress 中等号之后的空格。
错误是:
(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated\UXXXXXXXX escape
请您参考如下方法:
In Python strings, the backslash
"\"is a special character, also called the "escape" character. It is used in representing certain whitespace characters:"\t"is a tab,"\n"is a newline, and"\r"is a carriage return.
我认为错误是引用了您的地址,特别是其中的特殊字符 "\"。您不能在字符串中使用 "\",因为它会转义字符串。您可以尝试在您的地址中使用 "\\",我认为这应该可行。
请参阅此处以进一步阅读该主题:http://www.pitt.edu/~naraehan/python2/tutorial7.html
