Is there a known Win32 Tkinter error regarding displaying photos on canvas?

I notice a rather strange error with tkinter, and I wonder if this is due to the fact that python interacts with tcl, at least in Win32.

Here I have a super simple program that displays a gif image. It works great.

from Tkinter import *

canvas = Canvas(width=300, height=300, bg='white')   
canvas.pack()

photo=PhotoImage(file=sys.argv[1])
canvas.create_image(0, 0, image=photo, anchor=NW)  # embed a photo
print canvas
print photo

mainloop( )

Now I am modifying the program a bit to edit the canvas object inside the function. This time I just get a blank canvas.

# demo all basic canvas interfaces
from Tkinter import *

canvas = Canvas(width=300, height=300, bg='white')

canvas.pack()

def set_canvas(cv):
    photo=PhotoImage(file=sys.argv[1])
    cv.create_image(0, 0, image=photo, anchor=NW)  # embed a photo
    print cv
    print photo

set_canvas(canvas)
mainloop( )

The only difference between the two is that in one canvas the object is passed to the function instead of being used directly. Both print statements return identical results. I am wondering if, at least, a breakdown in the object model is possible at the tcl / python level.

Any thoughts people?

Thanks, / YGA

+3
1

, :

def set_canvas(cv):
    global photo # here!
    photo=PhotoImage(file=sys.argv[1])
    cv.create_image(0, 0, image=photo, anchor=NW)  # embed a photo
    print cv
    print photo

A PhotoImage Python, . photo , , , PhotoImage. set_canvas PhotoImage .

+6

Source: https://habr.com/ru/post/1703669/


All Articles