gpt4 book ai didi

python - Tkinter 在单个窗口中显示多个随机选择的图像

转载 作者:太空宇宙 更新时间:2023-11-04 05:28:03 24 4
gpt4 key购买 nike

代码:

#!/usr/bin/env python

import os
import sys
import random

from Tkinter import Label,Tk
from PIL import Image, ImageTk
import tkFileDialog


path = '/Users/InNov8/Desktop/broadcast/test'

# All images in the directory get stored in this list
images = []

for dirname, dirnames, filenames in os.walk(path):
for filename in filenames:
file = os.path.join(dirname, filename)
if '.jpg' in file.lower() or '.gif' in file.lower() or '.png' in file.lower():
images.append(file)


# random number of images to pick from within a range
random_max = random.randrange(2, 3)
print random_max

# list to hold the selected images
selection = []
counter = 0
while counter < random_max:
index = random.randrange(0, len(images))
selection.append(images[index])
print images[index]
images.pop(index)
counter +=1

print selection


# display the selected images in a Tkinter window
root = Tk()

for s in selection:
im = Image.open(s)
tkimage = ImageTk.PhotoImage(im)
myvar=Label(root,image = tkimage)
myvar.image = tkimage
myvar.pack()

root.mainloop()

我想从文件夹中随机选取图像并将它们显示在 Tkinter 窗口中。如果我喜欢这些图片,我会添加一个“开始”按钮来使用它们,否则我会添加一个“再次选择”按钮来重新选择。

由于所选图像的数量总是变化的,我试图找出一种动态的方法,这就是我在最后设置循环的原因。当然这是行不通的。

感谢任何帮助。谢谢。

最佳答案

发现我的原始代码确实有效,但问题是无法滚动。

这是添加了滚动条的最终代码:

#!/usr/bin/env python

import os
import sys
import random

from Tkinter import *
from PIL import Image, ImageTk
import tkFileDialog


path = '/Users/InNov8/Desktop/broadcast/test'

images = []

for dirname, dirnames, filenames in os.walk(path):
# print path to all subdirectories first.
# for subdirname in dirnames:
# print os.path.join(dirname, subdirname)

# print path to all filenames.
for filename in filenames:
file = os.path.join(dirname, filename)
if '.jpg' in file.lower() or '.gif' in file.lower() or '.png' in file.lower():
images.append(file)


random_max = random.randrange(2, 5)
print random_max

selection = []
counter = 0
while counter < random_max:
index = random.randrange(0, len(images))
selection.append(images[index])
print images[index]
images.pop(index)
counter +=1



print selection


## Main window
root = Tk()
## Grid sizing behavior in window
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
## Canvas
cnv = Canvas(root)
cnv.grid(row=0, column=0, sticky='nswe')
## Scrollbars for canvas
hScroll = Scrollbar(root, orient=HORIZONTAL, command=cnv.xview)
hScroll.grid(row=1, column=0, sticky='we')
vScroll = Scrollbar(root, orient=VERTICAL, command=cnv.yview)
vScroll.grid(row=0, column=1, sticky='ns')
cnv.configure(xscrollcommand=hScroll.set, yscrollcommand=vScroll.set)
## Frame in canvas
frm = Frame(cnv)
## This puts the frame in the canvas's scrollable zone
cnv.create_window(0, 0, window=frm, anchor='nw')
## Frame contents

for s in selection:
im = Image.open(s)
tkimage = ImageTk.PhotoImage(im)
myvar=Label(frm,image = tkimage)
myvar.image = tkimage
myvar.pack()

## Update display to get correct dimensions
frm.update_idletasks()
## Configure size of canvas's scrollable zone
cnv.configure(scrollregion=(0, 0, frm.winfo_width(), frm.winfo_height()))
## Go!


root.mainloop()

关于python - Tkinter 在单个窗口中显示多个随机选择的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38059145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com