更新时间:2023-02-22 15:40:51
正确转义路径字符串中的反斜杠.(或使用 r'raw string literal'
).
防止 PhotoImage 对象被垃圾收集.
Prevent PhotoImage object being garbage collected.
使用 file=...
选项指定文件名.
specify the filename using file=...
option.
def start(root):
startframe = tkinter.Frame(root)
canvas = tkinter.Canvas(startframe,width=1280,height=720)
startframe.pack()
canvas.pack()
# Escape / raw string literal
one = tkinter.PhotoImage(file=r'images\one.gif')
root.one = one # to prevent the image garbage collected.
canvas.create_image((0,0), image=one, anchor='nw')
更新
one = ...
和 root.one = one
两条语句可以合并为一条语句:
The two statements one = ...
and root.one = one
can be merged into one statement:
root.one = one = tkinter.PhotoImage(r'images\one.gif')