Python Forum
[Tkinter] Showing windows after one another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Showing windows after one another
#5
You can call Tk() more than once. I do that here to display all the png files in my games folder.
import tkinter as tk
from pathlib import Path

class ShowImage(tk.Tk):
    def __init__(self, image_file):
        super().__init__()
        image=tk.PhotoImage(file=image_file)
        tk.Label(self, image=image).pack()
        tk.Button(self, text='Next Image', command=self.destroy).pack()
        self.mainloop()

for image_file in Path("games").glob("*.png"):
    ShowImage(image_file)
This works because pressing the Next button destroys the only window, ending the tkinter session. To draw another tkinter window I need to call Tk() again. The program may draw dozens of windows, but each window is a completely new tkinter session.

But that is a silly way to display multiple images. Each time you press the Next button you get a new window. The next window might appear in a different location on the screen, forcing you to move the mouse to press the button again. A better solution is to use the one button to show all the images.

This program also displays image files in a directory, but it does it using the same window for each image.
import tkinter as tk
from pathlib import Path


class ShowImage(tk.Tk):
    """Loop through images in a folder."""
    def __init__(self, folder="."):
        super().__init__()
        self.label = tk.Label(self)
        self.label.pack()
        tk.Button(self, text='Next Image', command=self.next_image).pack()
        self.files = Path(folder).iterdir()
        self.next_image()

    def next_image(self):
        """Display next image from folder."""
        try:
            # Loop through files looking for image file to display.
            while image_file := next(self.files):
                try:
                    # Try to open file as an image.
                    self.image = tk.PhotoImage(file=image_file)
                    self.label["image"] = self.image
                    return
                except tk.TclError:
                    pass  # File was not an image file.  Try next file.
        except StopIteration:
            self.destroy()  # No more files.
A fun thing about this program is its use of "duck typing". Some of the files in the folder might not be image files. Some of the files might be image files but aren't in a format that is understood by tk.PhotoImage. Instead of trying to filter out what files to display and what to skip ahead of time, the program tries to open the file as an image. If this fails, the program catches the error and tries the next file.
Reply


Messages In This Thread
Showing windows after one another - by Ben123 - Feb-22-2024, 05:52 PM
RE: Showing windows after one another - by Ben123 - Feb-23-2024, 01:02 PM
RE: Showing windows after one another - by deanhystad - Feb-23-2024, 10:16 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020