如何在Tkinter中创建“下一步”按钮

时间:2015-05-10 11:21:52

标签: python tkinter

我必须制作照片浏览器GUI,我需要能够浏览照片,我将如何制作“下一个”按钮和“前一个”按钮。我是一个完全的初学者,所以任何帮助表示赞赏

2 个答案:

答案 0 :(得分:3)

尽可能简单:

1 - 创建一个主窗口,通常命名为root:

root = Tk()

2 - 添加用于显示图片的主框架(当前照片):

framePhoto = Frame(root, bg='gray50',relief = RAISED, width=800, height=600, bd=4)

3 - 添加两个按钮,Next&上一个:

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 - 您需要添加方法来处理您输入到应用程序的当前目录或目录中的所有图片,例如:

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 - 另一种打开和显示图像的方法:

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

这很简单,我可以向您解释,上面提到的代码是为了澄清。

答案 1 :(得分:2)

这是完整的代码,正如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')