I have to make a Photo Browser GUI, and I need to be able to browse through the photos, how would I Go about making a 'next' button and a 'previous'. I am a complete beginner so any help is appreciated
我必须制作一个照片浏览器GUI,我需要能够浏览照片,我将如何制作一个“下一个”按钮和一个“前一个”按钮。我是一个完整的初学者,所以任何帮助表示赞赏
2 个解决方案
#1
As simple as it can be:
尽可能简单:
1 - Create a master window, usually name root:
1 - 创建一个主窗口,通常命名为root:
root = Tk()
2 - Add a Main Frame for Displaying your Picture (current photo):
2 - 添加用于显示图片的主框架(当前照片):
framePhoto = Frame(root, bg='gray50',relief = RAISED, width=800, height=600, bd=4)
3 - Add two buttons, Next & Prev:
3 - 添加两个按钮,Next和Prev:
prevBtn = Button(self.framePhoto, text='Previous', command=lambda s=self: s.getImgOpen('prev'),
bg='blue', fg='red').place(relx=0.85, rely=0.99, anchor=SE)
nextBtn = Button(self.framePhoto, text='Next', command=lambda s=self: s.getImgOpen('next'),
bg='green', fg='black').place(relx=0.90, rely=0.99, anchor=SE)
4 - You need to add method to handle listing all pictures in your current directory or directory that you input to the application, example:
4 - 您需要添加方法来处理列出您输入到应用程序的当前目录或目录中的所有图片,例如:
def getImgList(self, path, ext):
imgList = [os.path.normcase(f) for f in os.listdir(path)]
imgList = [os.path.join(path, f) for f in imgList if os.path.splitext(f)[1] == ext]
self.images.extend(imgList)
5 - Another method to open and display the image:
5 - 打开和显示图像的另一种方法:
def getImgOpen(self,seq):
print 'Opening %s' % seq
if seq=='ZERO':
self.imgIndex = 0
elif (seq == 'prev'):
if (self.imgIndex == 0):
self.imgIndex = len(self.images)-1
else:
self.imgIndex -= 1
elif(seq == 'next'):
if(self.imgIndex == len(self.images)-1):
self.imgIndex = 0
else:
self.imgIndex += 1
self.masterImg = Image.open(self.images[self.imgIndex])
self.master.title(self.images[self.imgIndex])
self.masterImg.thumbnail((400,400))
self.img = ImageTk.PhotoImage(self.masterImg)
self.lbl['image'] = self.img
return
This is as simple as I can explain to you and the above mentioned piece of codes are for clarification.
这很简单,我可以向您解释,上面提到的代码是为了澄清。
#2
Here is the complete code, as explained by Iron Fist answer. I assembled it, so I it might be useful.
这是完整的代码,正如Iron Fist的解释所解释的那样。我组装了它,所以我可能会有用。
from Tkinter import *
import os
from PIL import ImageTk,Image
class Display(object):
def __init__(self):
self.images = [];
self.imgIndex = 0;
self.master= Tk()
self.framePhoto = Frame(self.master, bg='gray50',relief = RAISED, width=800, height=600, bd=4)
self.framePhoto.pack();
prevBtn = Button(self.framePhoto, text='Previous', command=lambda s=self: s.getImgOpen('prev')).place(relx=0.85, rely=0.99, anchor=SE)
nextBtn = Button(self.framePhoto, text='Next', command=lambda s=self: s.getImgOpen('next')).place(relx=0.90, rely=0.99, anchor=SE)
#prevBtn.pack();
#nextBtn.pack();
self.getImgList('test_2/test_2','.bmp')
mainloop()
def getImgList(self, path, ext):
imgList = [os.path.normcase(f) for f in os.listdir(path)]
imgList = [os.path.join(path, f) for f in imgList if os.path.splitext(f)[1] == ext]
self.images.extend(imgList)
#print self.images
def getImgOpen(self,seq):
print 'Opening %s' % seq
if seq=='ZERO':
self.imgIndex = 0
elif (seq == 'prev'):
if (self.imgIndex == 0):
self.imgIndex = len(self.images)-1
else:
self.imgIndex -= 1
elif(seq == 'next'):
if(self.imgIndex == len(self.images)-1):
self.imgIndex = 0
else:
self.imgIndex += 1
self.masterImg = Image.open(self.images[self.imgIndex])
self.master.title(self.images[self.imgIndex])
self.masterImg.thumbnail((400,400))
self.img = ImageTk.PhotoImage(self.masterImg)
label = Label(image=self.img)
label.image = self.img # keep a reference!
label.pack()
label.place(x=100,y=100)
#self.lbl['image'] = self.img
return
d = Display();
d.getImgOpen('next')
#1
As simple as it can be:
尽可能简单:
1 - Create a master window, usually name root:
1 - 创建一个主窗口,通常命名为root:
root = Tk()
2 - Add a Main Frame for Displaying your Picture (current photo):
2 - 添加用于显示图片的主框架(当前照片):
framePhoto = Frame(root, bg='gray50',relief = RAISED, width=800, height=600, bd=4)
3 - Add two buttons, Next & Prev:
3 - 添加两个按钮,Next和Prev:
prevBtn = Button(self.framePhoto, text='Previous', command=lambda s=self: s.getImgOpen('prev'),
bg='blue', fg='red').place(relx=0.85, rely=0.99, anchor=SE)
nextBtn = Button(self.framePhoto, text='Next', command=lambda s=self: s.getImgOpen('next'),
bg='green', fg='black').place(relx=0.90, rely=0.99, anchor=SE)
4 - You need to add method to handle listing all pictures in your current directory or directory that you input to the application, example:
4 - 您需要添加方法来处理列出您输入到应用程序的当前目录或目录中的所有图片,例如:
def getImgList(self, path, ext):
imgList = [os.path.normcase(f) for f in os.listdir(path)]
imgList = [os.path.join(path, f) for f in imgList if os.path.splitext(f)[1] == ext]
self.images.extend(imgList)
5 - Another method to open and display the image:
5 - 打开和显示图像的另一种方法:
def getImgOpen(self,seq):
print 'Opening %s' % seq
if seq=='ZERO':
self.imgIndex = 0
elif (seq == 'prev'):
if (self.imgIndex == 0):
self.imgIndex = len(self.images)-1
else:
self.imgIndex -= 1
elif(seq == 'next'):
if(self.imgIndex == len(self.images)-1):
self.imgIndex = 0
else:
self.imgIndex += 1
self.masterImg = Image.open(self.images[self.imgIndex])
self.master.title(self.images[self.imgIndex])
self.masterImg.thumbnail((400,400))
self.img = ImageTk.PhotoImage(self.masterImg)
self.lbl['image'] = self.img
return
This is as simple as I can explain to you and the above mentioned piece of codes are for clarification.
这很简单,我可以向您解释,上面提到的代码是为了澄清。
#2
Here is the complete code, as explained by Iron Fist answer. I assembled it, so I it might be useful.
这是完整的代码,正如Iron Fist的解释所解释的那样。我组装了它,所以我可能会有用。
from Tkinter import *
import os
from PIL import ImageTk,Image
class Display(object):
def __init__(self):
self.images = [];
self.imgIndex = 0;
self.master= Tk()
self.framePhoto = Frame(self.master, bg='gray50',relief = RAISED, width=800, height=600, bd=4)
self.framePhoto.pack();
prevBtn = Button(self.framePhoto, text='Previous', command=lambda s=self: s.getImgOpen('prev')).place(relx=0.85, rely=0.99, anchor=SE)
nextBtn = Button(self.framePhoto, text='Next', command=lambda s=self: s.getImgOpen('next')).place(relx=0.90, rely=0.99, anchor=SE)
#prevBtn.pack();
#nextBtn.pack();
self.getImgList('test_2/test_2','.bmp')
mainloop()
def getImgList(self, path, ext):
imgList = [os.path.normcase(f) for f in os.listdir(path)]
imgList = [os.path.join(path, f) for f in imgList if os.path.splitext(f)[1] == ext]
self.images.extend(imgList)
#print self.images
def getImgOpen(self,seq):
print 'Opening %s' % seq
if seq=='ZERO':
self.imgIndex = 0
elif (seq == 'prev'):
if (self.imgIndex == 0):
self.imgIndex = len(self.images)-1
else:
self.imgIndex -= 1
elif(seq == 'next'):
if(self.imgIndex == len(self.images)-1):
self.imgIndex = 0
else:
self.imgIndex += 1
self.masterImg = Image.open(self.images[self.imgIndex])
self.master.title(self.images[self.imgIndex])
self.masterImg.thumbnail((400,400))
self.img = ImageTk.PhotoImage(self.masterImg)
label = Label(image=self.img)
label.image = self.img # keep a reference!
label.pack()
label.place(x=100,y=100)
#self.lbl['image'] = self.img
return
d = Display();
d.getImgOpen('next')