Python 如何创建';下一个';Tkinter中的按钮
我必须制作一个照片浏览器GUI,我需要能够浏览照片,我该如何制作“下一步”按钮和“上一步”按钮。我是一个完全的初学者,因此非常感谢您提供的任何帮助尽可能简单: 1-创建主窗口,通常名称为root:Python 如何创建';下一个';Tkinter中的按钮,python,tkinter,Python,Tkinter,我必须制作一个照片浏览器GUI,我需要能够浏览照片,我该如何制作“下一步”按钮和“上一步”按钮。我是一个完全的初学者,因此非常感谢您提供的任何帮助尽可能简单: 1-创建主窗口,通常名称为root: root = Tk() 2-添加用于显示图片(当前照片)的主框架: 3-添加两个按钮,下一个和上一个: prevBtn = Button(self.framePhoto, text='Previous', command=lambda s=self: s.getImgOpen('prev'),
root = Tk()
2-添加用于显示图片(当前照片)的主框架:
3-添加两个按钮,下一个和上一个:
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-创建主窗口,通常名称为root:
root = Tk()
2-添加用于显示图片(当前照片)的主框架:
3-添加两个按钮,下一个和上一个:
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
这很简单,我可以向您解释,上面提到的代码是为了澄清。这是完整的代码,正如铁拳回答所解释的。我组装了它,所以我觉得它可能有用
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')
这是完整的代码,正如铁拳回答所解释的。我组装了它,所以我觉得它可能有用
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')
真的,在Tkinter之前你不知道列表和索引吗?只需使用一个图像列表,并在按下按钮时增加当前索引。你也可以使用模运算符在一个循环后包装图像。真的,你不知道Tkinter之前的列表和索引吗?只需使用一个图像列表,并在按下按钮时增加当前索引。你也可以使用模运算符在一个循环后包装图像。在我见过的其他地方,像这样的代码
def\uu init\uuuuu(self):
将缩进类显示(对象):
。也可以通过调用d=Display()代码>一个参数没有被传递,但是类显示它需要一个参数?在其他任何地方我都见过这样的代码def\uuuu init\uuuuuu(self):
将缩进类显示(对象):
。也可以通过调用d=Display()代码>未传递参数,但类显示它需要参数?