Python 3.x 按下按钮后python3更新Tkinter中的图像

Python 3.x 按下按钮后python3更新Tkinter中的图像,python-3.x,image,tkinter,Python 3.x,Image,Tkinter,我想创建一个非常简单的tkinter GUI。一张图片和一个按钮,称为“下一步”。单击该按钮将显示图像路径列表中的下一个图像 当我按下下一步按钮时,我了解如何显示一个图像并执行一个简单的功能: import tkinter as tk from PIL import ImageTk, Image #Function to do when buttom "NEXT" is clicked def display_new_image(): print("NEXT IMAGE, but h

我想创建一个非常简单的tkinter GUI。一张图片和一个按钮,称为“下一步”。单击该按钮将显示图像路径列表中的下一个图像

当我按下下一步按钮时,我了解如何显示一个图像并执行一个简单的功能:

import tkinter as tk
from PIL import ImageTk, Image

#Function to do when buttom "NEXT" is clicked
def display_new_image():
    print("NEXT IMAGE, but how ?")

root = tk.Tk()

# IMPORT PICTURE
#-------------------------------------
path = PATH_TO_PICTURE
image = Image.open(path)
img = ImageTk.PhotoImage(image)
#-------------------------------------

# ADD IMAGE TO THE GRID
tk.Label(image = img, relief=tk.RIDGE, width=400).grid(row=0,column=0)
tk.Button(text = "NEXT", relief=tk.RIDGE, width=40, command=display_new_image).grid(row=1,column=0)

tk.mainloop()
如何更新图片

编辑: 使用图像缩放:

import tkinter as tk
from PIL import ImageTk, Image

#Function to do when buttom "NEXT" is clicked
def display_new_image():
    print("NEXT IMAGE, but how ?")

root = tk.Tk()

# IMPORT AND SCALE PICTURE
#-------------------------------------
n=0.1 # Scaling factor to reduce image size
path = PATH_TO_PICTURE
image = Image.open(path)
[imageSizeWidth, imageSizeHeight] = image.size

newImageSizeWidth = int(imageSizeWidth*n)
newImageSizeHeight = int(imageSizeHeight*n) 

image = image.resize((newImageSizeWidth, newImageSizeHeight), Image.ANTIALIAS)
img = ImageTk.PhotoImage(image)
#-------------------------------------
# ADD IMAGE TO THE GRID

tk.Label(image = img, relief=tk.RIDGE, width=400).grid(row=0,column=0)
tk.Button(text = "NEXT", relief=tk.RIDGE, width=40, command=display_new_image).grid(row=1,column=0)

tk.mainloop()

这里有一个可能的解决方案,但我不确定,如果这是最好的方法。。。 至少,它是有效的。这篇文章的灵感来源于:

我不知道如何处理不同大小的图片

import tkinter as tk
from PIL import ImageTk, Image

class App:
    def __init__(self, window, window_title):
        self.window = window
        self.window.title(window_title)

        # List of Image Paths to display
        self.image_paths = [PATH1, PATH2, PATH3]
        # Image Counter -> To chose Path
        self.image_counter = 0
        # Image Scaling factor
        self.image_scalling_factor = 0.1

        # Resize Image -> img
        image = Image.open(self.image_paths[self.image_counter])
        [imageSizeWidth, imageSizeHeight] = image.size
        newImageSizeWidth = int(imageSizeWidth*self.image_scalling_factor)
        newImageSizeHeight = int(imageSizeHeight*self.image_scalling_factor) 
        resized_image = image.resize((newImageSizeWidth, newImageSizeHeight), Image.ANTIALIAS)
        self.img = ImageTk.PhotoImage(resized_image)

        # Create a canvas that can fit the above image
        self.canvas = tk.Canvas(window, width = newImageSizeWidth,  height = newImageSizeHeight)
        self.canvas.pack()
        # Add a PhotoImage to the Canvas
        self.canvas.create_image(0, 0, image=self.img, anchor=tk.NW)
        # Button that lets the user jump to next image the image
        self.btn_next_image=tk.Button(window, text="NEXT", width=50, command=self.next_image)
        self.btn_next_image.pack(anchor=tk.CENTER, expand=True)
        self.window.mainloop()

    # Callback for the "Blur" button
    def next_image(self):
        # Increase Image Counter
        self.image_counter += 1

        # Resize Image -> img
        image = Image.open(self.image_paths[self.image_counter])
        [imageSizeWidth, imageSizeHeight] = image.size
        newImageSizeWidth = int(imageSizeWidth*self.image_scalling_factor)
        newImageSizeHeight = int(imageSizeHeight*self.image_scalling_factor) 
        resized_image = image.resize((newImageSizeWidth, newImageSizeHeight), Image.ANTIALIAS)
        self.img = ImageTk.PhotoImage(resized_image)


        self.canvas.create_image(0, 0, image=self.img, anchor=tk.NW)

# Create a window and pass it to the Application object
App(tk.Tk(), "Picture NEXT GUI")