Python 如何将文件夹中的所有图像添加到按钮(在我的框架中)
我想将文件夹中的Python 如何将文件夹中的所有图像添加到按钮(在我的框架中),python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,我想将文件夹中的.jpg图像存储在数组中,然后使用它们将其添加到Python框架中的按钮中 我使用的是Python 3.4.3中的tkinter和PIL库。我使用的是windows 8.1操作系统。我想加载一个文件夹的图像,其中包含几行代码,我还需要网格的元素边框,以便像单个图像一样显示它们。下面是一些代码,让您开始学习 为了简化测试,我编写了一个小程序,使用PIL生成彩色方块并将其保存到文件夹中。我在Linux上使用Python 2.6.6测试了这些程序。它们应该可以在Python3上工作(假
.jpg
图像存储在数组中,然后使用它们将其添加到Python框架中的按钮中
我使用的是Python 3.4.3中的tkinter和PIL库。我使用的是windows 8.1操作系统。我想加载一个文件夹的图像,其中包含几行代码,我还需要网格的元素边框,以便像单个图像一样显示它们。下面是一些代码,让您开始学习 为了简化测试,我编写了一个小程序,使用PIL生成彩色方块并将其保存到文件夹中。我在Linux上使用Python 2.6.6测试了这些程序。它们应该可以在Python3上工作(假设Tkinter和PIL正确安装在您的系统上),但是您需要将Tkinter作为tk导入
更改为
在tk\u frame\u grid.py中将tkinter作为tk导入
该程序生成30个彩色方块,将它们保存到当前目录中的“方块”文件夹中。在运行程序之前,您需要创建“正方形”
PIL_2彩色正方形.py
#!/usr/bin/env python
''' Create a grid of Tkinter Buttons with PIL images in a Frame
See http://stackoverflow.com/q/31489121/4014959
Written by PM 2Ring 2015.07.18
'''
from __future__ import print_function
import os
import Tkinter as tk
from PIL import Image, ImageTk
class FrameDemo(object):
''' A Frame containing a grid of image Buttons '''
def __init__(self, rows, columns, images):
images = iter(images)
master = tk.Tk()
master.title("Image Buttons in a Frame")
frame = tk.Frame(master)
frame.pack()
for y in range(rows):
for x in range(columns):
#Get the next image from the list and
#convert it to a TK Photo object
name, img = next(images)
photo = ImageTk.PhotoImage(img)
#Create the button & put it in the Frame
b = tk.Button(frame, image=photo)
b.grid(row=y, column=x)
#Add a call-back function
func = lambda t=name: self.button_cb(t)
b.config(command=func)
#We have to save a reference to the photo or it gets
#garbage-collected, so we attach it as a button attribute
b.photo = photo
master.mainloop()
def button_cb(self, name):
print("'%s' button pressed" % name)
def main():
image_folder = 'squares'
#Load all files in image_folder as PIL images
#and put them into a list
images = [(name, Image.open(os.path.join(image_folder, name)))
for name in sorted(os.listdir(image_folder))]
gui = FrameDemo(rows=5, columns=6, images=images)
if __name__ == '__main__':
main()
#/usr/bin/env python
''创建从一种颜色渐变到另一种颜色的正方形并保存
由PM 2Ring 2015.07.18撰写
'''
从PIL导入图像
从itertools导入置换
def颜色方格(大小、颜色对、基本名称、F类型):
#制作合成面具
m=255.0/(2*尺寸-2)
r=范围(大小)
掩码=图像。新建('L',(大小,大小))
mask.putdata([int(m*(x+y))表示r中的y表示r中的x])
对于枚举(颜色对)中的计数(c0,c1):
im0=图像。新建('RGB',(大小,大小),颜色=c0)
im1=图像。新建('RGB',(大小,大小),颜色=c1)
im=Image.composite(im0、im1、mask)
im.save(“%s%03d%s%”(basename,count,ftype))
def main():
尺寸=64
颜色=(‘红色’、‘黄色’、‘绿色’、‘青色’、‘蓝色’、‘洋红’)
颜色对=排列(颜色,2)
basename='正方形/平方'
ftype='.png'
颜色方格(大小、颜色对、基本名称、F类型)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
该程序首先使用命名目录中的图像文件,用PIL图像填充列表;这些图像可以是PIL可以读取的任何文件格式。然后,它创建一个Tkinter窗口,其中包含一个框架,其中包含使用图像的按钮网格。没有错误检查,因此不要将非图像文件放入“squares”文件夹
您需要指定轴网尺寸。如果没有足够的图像填充网格,则会出现StopIteration
错误
tk\u frame\u grid.py
#!/usr/bin/env python
''' Create a grid of Tkinter Buttons with PIL images in a Frame
See http://stackoverflow.com/q/31489121/4014959
Written by PM 2Ring 2015.07.18
'''
from __future__ import print_function
import os
import Tkinter as tk
from PIL import Image, ImageTk
class FrameDemo(object):
''' A Frame containing a grid of image Buttons '''
def __init__(self, rows, columns, images):
images = iter(images)
master = tk.Tk()
master.title("Image Buttons in a Frame")
frame = tk.Frame(master)
frame.pack()
for y in range(rows):
for x in range(columns):
#Get the next image from the list and
#convert it to a TK Photo object
name, img = next(images)
photo = ImageTk.PhotoImage(img)
#Create the button & put it in the Frame
b = tk.Button(frame, image=photo)
b.grid(row=y, column=x)
#Add a call-back function
func = lambda t=name: self.button_cb(t)
b.config(command=func)
#We have to save a reference to the photo or it gets
#garbage-collected, so we attach it as a button attribute
b.photo = photo
master.mainloop()
def button_cb(self, name):
print("'%s' button pressed" % name)
def main():
image_folder = 'squares'
#Load all files in image_folder as PIL images
#and put them into a list
images = [(name, Image.open(os.path.join(image_folder, name)))
for name in sorted(os.listdir(image_folder))]
gui = FrameDemo(rows=5, columns=6, images=images)
if __name__ == '__main__':
main()
你需要在你的问题中加入更多的细节!你到底想做什么还不清楚。您需要告诉我们您使用的是哪个GUI库和Python的哪个版本。你更有可能得到帮助,所以如果你发布一个显示你尝试过的东西的帖子。我希望我的相框有很多图片按钮。连接到其他框架。您应该将该信息编辑到您的问题中。但不清楚“连接到其他帧”是什么意思。如果您不告诉我们您使用的是哪个GUI系统,我们也帮不了您多少忙。您使用的是tkinter、gtk、pyqt还是其他什么?如果你不澄清你的问题,最好包括一些代码,那么在你修复它之前,它可能会被搁置。我想你是在用问题的语言使用tkinter
(Python 2:tkinter
)。但是我很难理解“使用它们将它添加到我的Python框架中的按钮中”。由于没有代码,这一切都发生在我们的想象中,这就是为什么指导方针建议您提供损坏的代码。尝试一下,然后说出您不理解的内容!您在解决问题的哪一部分?您知道如何创建窗口吗?您知道如何添加带有图像的标签吗?您知道如何阅读文档吗你需要什么帮助?