2017.08.16 利用python构建简易图片浏览器

来源:互联网 发布:mac 连接文件服务器 编辑:程序博客网 时间:2024/06/05 19:22
import tkinter as tk,osclass Application(tk.Frame):    def __init__(self,master=None):        self.files=os.listdir(r'D:\Picture')        self.index=0        self.img=tk.PhotoImage(file=r'D:\Picture'+'\\'+self.files[self.index])        tk.Frame.__init__(self,master)        self.pack()        self.createWidgets()    def createWidgets(self):        self.lblImage=tk.Label(self,width=300,height=300)        self.lblImage['image']=self.img        self.lblImage.pack()        self.f=tk.Frame()        self.f.pack()        self.btnPrev=tk.Button(self.f,text='上一张',command=self.prev)        self.btnPrev.pack(side=tk.LEFT)        self.btnNext=tk.Button(self.f,text='下一张',command=self.next)        self.btnNext.pack(side=tk.LEFT)    def prev(self):        self.showfile(-1)    def next(self):        self.showfile(1)    def showfile(self,n):        self.index+=n        if self.index<0:            self.index=len(self.files)-1        if self.index>(len(self.files)-1):            self.index=0        self.img=tk.PhotoImage(file=r'D:\Picture'+'\\'+self.files[self.index])        self.lblImage['image']=self.img                root=tk.Tk()root.title('简易图片浏览器')app=Application(master=root)app.mainloop()