Snippets Collections
from customtkinter import *
from PIL import Image
 
class App(CTk):
    def __init__(self):
        super().__init__()
        self.title("Personal Gallery")
        self.iconbitmap("images\\gallery.ico")  
        self.configure(fg_color="#181b1f")
 
        # setting up counter and images
        self.counter = 0
        # getting all the images in a list
        # If you have a lot of them, I suggest store them in a different file
        self.images = [CTkImage(light_image=Image.open("images\\forest.jpg"),size=(496.9,331.3)), 
                       CTkImage(light_image=Image.open("images\\moon.jpg"),size=(390.6,260.4)), 
                       CTkImage(light_image=Image.open("images\\mountain.jpg"),size=(654.1,436.1)), 
                       CTkImage(light_image=Image.open("images\\stone.jpg"),size=(593.7,416)), 
                       CTkImage(light_image=Image.open("images\\tree.jpg"),size=(624,416))]
         
        # images for buttons (forward and backward buttons)
        self.forward_img = CTkImage(light_image=Image.open("images\\forward.png"), size=(50,50))
        self.backward_img = CTkImage(light_image=Image.open("images\\backward.png"), size=(50,50))
 
        # label to show images
        self.il = CTkLabel(self,text="",image=self.images[self.counter])
        self.il.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
 
        # buttons
        self.front = CTkButton(self, text="", image=self.forward_img, fg_color="transparent", hover_color="#181b1f", command=self.next_command)
        self.back = CTkButton(self, text="", image=self.backward_img, fg_color="transparent", hover_color="#181b1f", command=self.previous)
 
        self.front.grid(row=1, column=2, padx=10, pady=10)
        self.back.grid(row=1, column=0, padx=10, pady=10)
 
        # exit button
        self.qb = CTkButton(self, text="Exit", fg_color="red", hover_color="pink", text_color="white", command=self.quit)
        self.qb.grid(row=1, column=1, padx=10, pady=10)
 
        # status bar
        self.status = CTkLabel(self, text=f"Image {self.counter + 1} of {len(self.images)}")
        self.status.grid(row=2, column=2, padx=25)
 
    def next_command(self):
        # To keep counter from being more than 4
        # (4+1)%5 = 0
        # ensures cycle
        self.counter = (self.counter + 1) % len(self.images)
        self.update()
 
    def previous(self):
        # To keep counter from being less than 0
        # (0-1)%5 = 4
        # ensures cycle
        self.counter = (self.counter - 1) % len(self.images)
        self.update()
 
    def update(self):
        # removes label from the grid
        self.il.grid_forget()
        # updating image
        self.il = CTkLabel(self,text="",image=self.images[self.counter])
        self.il.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
 
        # updating status bar
        self.status = CTkLabel(self, text=f"Image {self.counter + 1} of {len(self.images)}")
        self.status.grid(row=2, column=2, padx=25)
 
if __name__ == '__main__':
    app = App()
    app.mainloop()
star

Wed Jul 19 2023 15:00:17 GMT+0000 (Coordinated Universal Time) https://python-hub.com/day-10-your-own-gallery-in-python/

#python #python-hub #day10

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension