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

时间:2016-06-27 16:43:45

标签: python tkinter

代码:

#!/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窗口中显示它们。如果我喜欢这些图片,我会添加一个&#34; go&#34;按钮使用它们,否则我将再次添加&#34;再次选择&#34;按钮重新选择。

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

感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

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

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

#!/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()