Python + tkinter does not display pictures using canvas

The problem of using canvas in python + tkinter to switch not to display pictures

1. Use of canvas

There are two important points to note about the create_image method of the canvas object, one is the format, and the second is to keep the continuous reference:

1.This should be a PhotoImage or BitmapImage, or a compatible object (such as the PIL PhotoImage).

This should be a PhotoImage or BitmapImage, or a compatible object (such as PIL PhotoImage).

2.The application must keep a reference to the image object.

The application must retain a reference to the image object.

Second, the solution

Declare global variables in advance, then use global in the method to declare the variables as global variables

image = None
im = None


def method():
    global image
    global im
    image = Image.open("img.jpg")  
    im = ImageTk.PhotoImage(image)

 

Published 128 original articles · Like 132 · Visits 170,000+

Guess you like

Origin blog.csdn.net/yql_617540298/article/details/101478896