简体   繁体   English

tkinter canvas 图像未显示

[英]tkinter canvas image not displaying

I have a simple canvas being created in a function, and i would like an image displayed on the canvas.我在 function 中创建了一个简单的 canvas,我想在 canvas 上显示图像。

def start(root):
    startframe = tkinter.Frame(root)
    canvas = tkinter.Canvas(startframe,width=1280,height=720)

    startframe.pack()
    canvas.pack()

    one = tkinter.PhotoImage('images\one.gif')
    canvas.create_image((0,0),image=one,anchor='nw')

when i run the code i get a blank 1280x720 window, no image.当我运行代码时,我得到一个空白的 1280x720 window,没有图像。

i have looked at the following website:http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm but i do not understand how to apply their example to my situation (i dont know what to create a reference to or how to create a reference, if that is my problem).我查看了以下网站:http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm但我不明白如何将他们的示例应用于我的情况(我不知道如果这是我的问题,要创建什么引用或如何创建引用)。 I have also looked at some stack overflow questions but they did not help either.我还查看了一些堆栈溢出问题,但它们也没有帮助。

  1. Escape backslashes in path string correctly.正确转义路径字符串中的反斜杠。 (or use r'raw string literal' ). (或使用r'raw string literal' )。

  2. Prevent PhotoImage object being garbage collected.防止 PhotoImage 对象被垃圾收集。

  3. specify the filename using file=... option.使用file=...选项指定文件名。


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')

UPDATE更新

The two statements one = ... and root.one = one can be merged into one statement:两个语句one = ...root.one = one可以合并为一个语句:

    root.one = one = tkinter.PhotoImage(r'images\one.gif')

How about canvas.update() ? canvas.update()怎么样? I was suffering a similar problem.我遇到了类似的问题。 I am using grid, so instead of .pack I needed to use .update .我使用的网格,所以不是.pack我需要使用.update

I had the situation when image didn`t show up, when I call it in function, but otherwise everything was okay.当我在 function 中调用它时,我遇到了图像没有显示的情况,但其他一切都很好。 That is my function那是我的 function

def ask_for_file():
    f_types = [('PNG Images', '*.png')]
    filename = askopenfilename(filetypes=f_types)
    img = ImageTk.PhotoImage(file=filename)
    canvas.config(height=img.height(), width=img.width())
    canvas.create_image(0, 0, anchor=NW, image=img)

My solution was to add img = None as global variable and change it inside function.我的解决方案是添加img = None作为全局变量并在 function 中更改它。 It worked有效

img = None


def ask_for_file():
    global img
    f_types = [('PNG Images', '*.png')]
    filename = askopenfilename(filetypes=f_types)
    img = ImageTk.PhotoImage(file=filename)
    canvas.config(height=img.height(), width=img.width())
    canvas.create_image(0, 0, anchor=NW, image=img)

    
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM