Как создать несколько слайд-шоу в одном окне в Python - PullRequest
0 голосов
/ 28 октября 2019

Я использую библиотеку tkinter в python. Я хочу создать несколько слайд-шоу в одном окне внутри двух полотен. Изображения показывают, но 2-е изображение распространяется на все окно. Как вписать изображения только в полотна?

from itertools import cycle
from glob import iglob
import tkinter as tk
import os
from PIL import ImageTk, Image


class App(tk.Tk):
    '''Tk window/label adjusts to size of image'''
    def __init__(self, image_files, delay):
        '''-----------------------------------------------------------'''
        x,y,max_x,max_y,h=0,0,720,1024,0
        part=int((max_y-(y+h))/3)

        small_width,small_height=140,140
        '''-----------------------------------------------------------'''

        # the root will be self
        tk.Tk.__init__(self)
        # set x, y position only
        self.geometry('+{}+{}'.format(x, y))
        self.delay = delay
        # allows repeat cycling through the pictures
        # store as (img_object, img_name) tuple
        self.pictures = cycle((ImageTk.PhotoImage(Image.open(image)), image) for image in image_files) #cycle(image_files)#
        self.picture_display = tk.Label(self)       
        self.picture_display.pack()             
        self.picture_display1 = tk.Label(self)
        self.picture_display1.pack()

        '''-----------------------------------------------------------'''
        self.canvas1 = tk.Canvas(self, bg="blue", height=part, width=(max_y/2))
        self.canvas1.place(x=0,y=0)
        self.canvas2 = tk.Canvas(self, bg="green", height=part, width=(max_y/2))
        self.canvas2.place(x=int(max_x/2),y=int(y+h))
        '''-----------------------------------------------------------'''

    def show_slides(self):
        try:
            '''cycle through the images and show them'''
            img_object, img_name = next(self.pictures)
            self.picture_display.config(image=img_object)
            self.canvas1.create_image(0, 0, image=img_object, anchor=tk.NW)
            self.title(img_name)

            img_object1, img_name1 = next(self.pictures)
            self.picture_display1.config(image=img_object1)
            self.canvas2.create_image(0, 0, image=img_object1, anchor=tk.NW)
            self.title(img_name1)

            self.after(self.delay, self.show_slides)


        except Exception as e:
            print(e)

    def run(self):
        self.mainloop()


if __name__=='__main__':
    # set milliseconds time between slides
    delay = 1000
    # get a series of gif images you have in the working folder
    # or use full path, or set directory to where the images are
    image_files = [f for f in iglob('**/*.jpg', recursive=True) if os.path.isfile(f)]
    #print(image_files)
    # upper left corner coordinates of app window

    app = App(image_files, delay)
    app.show_slides()
    app.run()

Два изображения, которые показывают, должны быть разными. В качестве первого шага я беру изображения из 1 папки, она должна быть из другой папки. Я планировал проверить с именем папки. Предположим, у меня есть папка collection1 и collection2. И я хочу взять изображения из 1-й папки. я проверяю так:

 image_files = [f for f in iglob('**/*.jpg', recursive=True) if os.path.isfile(f) if 'collection1' in f]

но здесь проблема в том, что если имя папки изменится, моя программа не будет выполнена. Есть ли какой-нибудь гибкий способ?

...