0

I am working on a picture thing, and I want the user to be able to select an image with buttons. But instead, nothing changes. How do you do that?

import tkinter as tk
from PIL import ImageTk, Image
n = 0
x = 0
def func(n):
    is_ = [
        Image.open(r"image_file1.png"),
        Image.open(r"image_file2.png"),
        Image.open(r"image_file3.png")
    ]
    global x
    x = n
    i2 = ImageTk.PhotoImage(is_[x])
    c2.create_image(50, 50, image = i2)
    
root = tk.Tk()
i = Image.open(r"C:\Users\User\Desktop\0mega.patch\2. Python\LucasPatrykRiley\Pfps\Clear Background.png")
i2 = ImageTk.PhotoImage(i)


c = tk.Canvas(root, height = 600, width = 600)
c.pack()

f = tk.Frame(root)

c2 = tk.Canvas(f, height = 100, width = 100, bg = "Blue")
c2.create_image(50, 50, image = i2)
c2.pack()

b = tk.Button(f, text = ">", command = func(x + 1))
b.pack()

b2 = tk.Button(f, text = "<", command = func(x - 1))
b2.pack()


c.create_window(200, 500, window = f)

root.mainloop()

is_ is the list of all images;
x is the user choice which gets updated so you can take the previous / next image;
c is the canvas where everything is positioned. I chose canvas bc the canvas can contain images.
This is a snippet of the main file, where the canvas has an image of the actual background.
The widgets are packed into the f Frame, which get placed into the c canvas

How can I make the images change with the button press?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Katalysmus
  • 41
  • 7
  • 1
    Why do You ask `can` and not `how` because in vast majority of cases the answer to `can` is YES. And so in this case. The answer to _Can you change the picture in a gui with a button press?_ is YES. Anyhow does this answer Your question: [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – Matiiss May 14 '21 at 13:18
  • Just so You know, Labels and Buttons can too contain images. And I think it would be actually maybe easier to change an image for a label than canvas – Matiiss May 14 '21 at 13:21
  • The issue: You create all the images in a function and when the function is done that list that contained all those images gets garbage collected and since no reference was created it all gets deleted. See previous comment for a similar issue. – Matiiss May 14 '21 at 13:27
  • 2
    Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – Matiiss May 14 '21 at 13:27

1 Answers1

0

Can you change the picture in a gui with a button press?

the user choice which gets updated so you can take the previous / next image

The func(n) function doesn't do anything.

The problem can be fixed and easier way to do.

  • On line 4, add list_images are used to store multiple items in a single variable.
  • On line 5, add declare index variable.
  • Create two function for previous and next.
  • Add c.itemconfig for updating previous and next functions.
  • On line 31, uncertainty your path Image.open.

Snippet modified:

import tkinter as tk
from PIL import ImageTk, Image

list_images = ["image_file1.png", "image_file2.png", "image_file3.png", "image_file4.png"]
index: int = 0

def next_image():
    global index, show_image
    if index == len(list_images)-1:
        return
    index += 1
    show_image = tk.PhotoImage(file=list_images[index])
    c.itemconfig(image_canvas, image=show_image)

def previous_image():
    global index, show_image
    if index == 0:
        return
    index -= 1
    show_image = tk.PhotoImage(file=list_images[index])
    c.itemconfig(image_canvas, image=show_image)    
     
root = tk.Tk()

c = tk.Canvas(root, height = 200, width = 600)

show_image = tk.PhotoImage(file=list_images[index])
image_canvas = c.create_image(90, 50, image=show_image)
c.pack()

i = Image.open(r"C:\Users\User\Desktop\0mega.patch\2. Python\LucasPatrykRiley\Pfps\Clear Background.png")
i2 = ImageTk.PhotoImage(i)

f = tk.Frame(root)

c2 = tk.Canvas(root, height = 100, width =120, bg="Blue")
c2.create_image(60,50, image = i2)
c2.pack()

b = tk.Button(root, text=">", command=next_image)
 
b2 = tk.Button(root, text="<", command=previous_image)
 
b2.pack()
b.pack()

root.mainloop()

Screenshot for next and previous button:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19