Python tkinter GUI dynamically changes images

WBOY
Release: 2024-02-09 14:45:04
forward
1140 people have browsed it

Python tkinter GUI动态改变图像

Question content

I come from java and just want to create a graphical user interface with pictures, but the pictures are changing dynamically and I don't know what it will be Which image...but it doesn't work, I can only create an image and can't change it outside root.mainloop() and get tons of error messages... Does anyone know how to dynamically change the image? Thank you so much

import tkinter as tk
from PIL import Image, ImageTk

def add_image_to_gui(root, image_path):
    # Load image
    image = Image.open(image_path)
    # Convert image to Tkinter-compatible format
    tk_image = ImageTk.PhotoImage(image)
    # Create label and display image
    image_label = tk.Label(root, image=tk_image)
    image_label.image = tk_image  # Keep reference to the image to prevent it from being garbage collected
    image_label.pack()

def create_gui(window_title, window_size, image_path):
    # Create Tkinter window
    root = tk.Tk()
    root.title(window_title)
    root.geometry(window_size)

    # Add image
    add_image_to_gui(root, image_path)

    # Start Tkinter event loop
    root.mainloop()

    return root

if __name__ == "__main__":
    window_title = "Simple GUI"
    window_size = "1495x1020"
    image_path = "Bilder/Hauptfenster.png"
    root = create_gui(window_title, window_size, image_path)
Copy after login

chatgpt, googled but no one seems to be having the same problem as me...


Correct Answer


To be updated after a while To switch images, you need to call some type of function to change it. Since this gui has no buttons, the easiest way is to schedule future updates using tkinter.tk().after and let that function change the label image.

Here is some sample code showing how to achieve this with minimal modifications:

EDIT: Updated with additional information in comments, another function will provide the file paths and the call to function_that_yields_new_image_paths will be replaced.

import tkinter as tk
from PIL import Image, ImageTk

def add_image_to_gui(root):
    image_label = tk.Label(root)
    image_label.pack()
    return image_label

def update_image(root, image_label):
    image = Image.open(function_that_yields_new_image_paths())
    tk_image = ImageTk.PhotoImage(image)
    image_label.configure(image=tk_image)
    image_label.image = tk_image
    root.after(1000, update_image, root, image_label)

def function_that_yields_new_image_paths():
    import numpy
    image_path1 = "a.png"
    image_path2 = "b.png"
    return numpy.random.choice((image_path1, image_path2))

def create_gui(window_title, window_size):
    # Create Tkinter window
    root = tk.Tk()
    root.title(window_title)
    root.geometry(window_size)

    # Add image
    image_label = add_image_to_gui(root)
    root.after(1, update_image, root, image_label)
    # Start Tkinter event loop
    root.mainloop()

    return root

if __name__ == "__main__":
    window_title = "Simple GUI"
    window_size = "1495x1020"

    root = create_gui(window_title, window_size)
Copy after login

If you have any questions, please let me know.

The above is the detailed content of Python tkinter GUI dynamically changes images. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!