I created a slideshow with Python3 & Tkinter. It works fine, but only if I define the path to the image folder inside the code. But the people I am going to share this program with don't know how to edit the code. So, I want another Tkinter window that pops up when you start the program, then asks you to define the path to your image folder. You copy and paste the path (of your image folder) into a text box inside the tkinter window. I will put my slideshow code underneath.
CODE:
import tkinter as Tk
from PIL import Image, ImageTk
import random
import glob
class GUI:
def __init__(self, mainwin):
self.mainwin = mainwin
self.mainwin.title("Our Photos")
self.mainwin.configure(bg="yellow")
self.counter = 0
self.frame = Tk.Frame(mainwin)
self.frame.place(relheight=0.85, relwidth=0.9, relx=0.05, rely=0.05)
self.img = Tk.Label(self.frame)
self.img.pack()
self.pic_list = glob.glob("/home/maheswar/Pictures/*")
self.colours = ['snow', 'ghost white', 'white smoke', 'gainsboro', 'floral white', 'old lace',
'linen', 'antique white', 'papaya whip', 'blanched almond']
self.colour()
self.pic()
def colour(self):
selected = random.choice(self.colours)
self.mainwin.configure(bg=selected)
root.after(4000, self.colour)
def pic(self):
filename = self.pic_list[self.counter]
image = Image.open(filename)
real_aspect = image.size[0]/image.size[1]
width = int(real_aspect * 800)
image = image.resize((width, 800))
self.photo = ImageTk.PhotoImage(image)
self.img.config(image=self.photo)
self.counter = 1
if self.counter >= len(self.pic_list):
self.counter = 0
root.after(2000, self.pic)
root = Tk.Tk()
myprog = GUI(root)
root.geometry("1000x1000")
root.mainloop()
CodePudding user response:
You can make use of askdirectory
with tkinter
:
from tkinter import filedialog
class GUI:
def __init__(self, mainwin):
...
dir = filedialog.askdirectory(parent=mainwin, title='Title of the box',
initialdir='path/of/initial/directory')
self.pic_list = glob.glob(f"{dir}/*")
Note: If you are looking to pick up images, then you can add extensions to your glob
, like glob.glob(f"{dir}/*.png')
and this will choose all png images inside the dir
instead of all the files.