Add picture with imagetk for multiple windows frames in tkinter gui with python

When working with tkinter GUI in Python, it is common to encounter the need to add pictures to multiple window frames. One way to achieve this is by using the imagetk module. In this article, we will explore three different approaches to solve this problem.

Approach 1: Using a Class

from tkinter import Tk, Frame, Label
from PIL import ImageTk, Image

class Window(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

    def init_window(self):
        self.master.title("Multiple Windows")
        self.pack(fill='both', expand=True)

        # Load image
        image = Image.open("image.jpg")
        photo = ImageTk.PhotoImage(image)

        # Create labels with images
        label1 = Label(self, image=photo)
        label1.image = photo
        label1.pack()

        label2 = Label(self, image=photo)
        label2.image = photo
        label2.pack()

root = Tk()
app = Window(root)
root.mainloop()

In this approach, we create a class called “Window” that inherits from the tkinter Frame class. Inside the class, we define the init_window method, which sets up the window and adds the image labels. We load the image using the PIL library and create a PhotoImage object from it. Then, we create two labels and assign the image to them. Finally, we create an instance of the Window class and run the main event loop.

Approach 2: Using Functions

from tkinter import Tk, Frame, Label
from PIL import ImageTk, Image

def create_window():
    root = Tk()
    root.title("Multiple Windows")

    # Load image
    image = Image.open("image.jpg")
    photo = ImageTk.PhotoImage(image)

    # Create labels with images
    label1 = Label(root, image=photo)
    label1.image = photo
    label1.pack()

    label2 = Label(root, image=photo)
    label2.image = photo
    label2.pack()

    root.mainloop()

create_window()

In this approach, we define a function called “create_window” that creates a new tkinter window and adds the image labels. We load the image and create a PhotoImage object in the same way as in the previous approach. Then, we create the labels and assign the image to them. Finally, we run the main event loop of the window.

Approach 3: Using a List

from tkinter import Tk, Frame, Label
from PIL import ImageTk, Image

def create_window():
    root = Tk()
    root.title("Multiple Windows")

    # Load image
    image = Image.open("image.jpg")
    photo = ImageTk.PhotoImage(image)

    # Create labels with images
    labels = []
    for _ in range(2):
        label = Label(root, image=photo)
        label.image = photo
        label.pack()
        labels.append(label)

    root.mainloop()

create_window()

In this approach, we define a function called “create_window” that creates a new tkinter window and adds the image labels. We load the image and create a PhotoImage object in the same way as in the previous approaches. Instead of creating individual label variables, we create a list called “labels” and use a loop to create and pack the labels. We also append each label to the list. Finally, we run the main event loop of the window.

After analyzing the three approaches, it can be concluded that the best option depends on the specific requirements of the project. If the application requires more complex functionality and customization, Approach 1 using a class provides a more organized and modular solution. On the other hand, if simplicity and ease of use are the main priorities, Approach 2 using functions or Approach 3 using a list can be more suitable. It is recommended to choose the approach that best fits the overall structure and goals of the project.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents