简单图片浏览器

news/2024/10/17 14:13:49/

本示例用于循环显示GIF图片,调用Python的标准库tkinter,如果要做更高端的图像处理应该采用PIL库。

import tkinter as tk,os  #导入库
class Application(tk.Frame):   #定义GUI的应用程序类,派生于Frmaedef __init__(self,master=None):   #构造函数,master为父窗口self.files=os.listdir(r'f:\jpg')   #获取图像文件名列表self.index=0    #图片索引,初始显示第一张self.img=tk.PhotoImage(file=r'f:\jpg'+'\\'+self.files[self.index])tk.Frame.__init__(self,master)    #调用父类的构造函数self.pack()  #调整显示的位置和大小self.createWidget()  #类成员函数,创建子组件def createWidget(self):self.lblImage=tk.Label(self,width=300,height=300)  #创建Label组件以显示图像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 += nif 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'f:\jpg'+'\\'+self.files[self.index])self.lblImage['image']=self.imgroot=tk.Tk()   #创建一个Tk根窗口组件
root.title("简单图片浏览器")   #设置窗口标题
app=Application(master=root)   #创建Application的对象实例
app.mainloop()   #事件循环

运行结果:

     

注意,此处的PhotoImage只能处理.gif格式的图像,如果用PhotoImage打开JPG或者BMP格式会出现

_tkinter.TclError: couldn't recognize data in image file "f:\jpg\1.jpg等错误。


http://www.ppmy.cn/news/150212.html

相关文章

Gallery 简易图片浏览

1. build.gradle, AndroidManifast.xml, 配置文件添加引用 1.1 dependencies 中添加引用库 dependencies {//http 请求implementation com.android.volley:volley:1.2.1//上拉刷新implementation androidx.swiperefreshlayout:swiperefreshlayout:1.1.0//加载网络地址图片impl…

照片浏览

照片浏览器&#xff1a; 1&#xff0c;SDPhotoBrowser 链接&#xff1a; https://github.com/gsdios/SDPhotoBrowser 介绍&#xff1a;中国人提供的源码&#xff0c;是在sdimage上进行开发的&#xff1b;可以查看网络图片&#xff0c;并且有图片缓存功能&#xff0c;加载一次后…

XnViewMP图片浏览器

今天小编给大家带来的是XnViewMP图片浏览器&#xff0c;这是一款非常棒的完全免费图片浏览器&#xff0c;同时支持多种语言&#xff0c;并且是纯绿色免安装&#xff0c;即下即用。软件支持100多种图片格式&#xff0c;还具有浏览器、幻灯片、屏幕捕捉、缩略图制作、批处理转换、…

页面图片浏览

在页面上浏览图片&#xff0c;实现图片的拖放 <html><head><meta http-equiv"Content-Type" content"text/html; charsetgb2312"><title>位图浏览器</title><style type"text/css"><!--#pic { width:…

Js图片浏览器

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns"http://www.w3.org/1999/xhtml"> <head> <title>Js图片浏览器</title&…

照片浏览器

昨天写了个图片浏览器&#xff0c;很简单&#xff0c;主要就是用一个JFileChooser导入一个文件&#xff0c;然后在文件父目录下导入下一个 或者上一个图像。 东西做好了&#xff0c;但是还有一个问题&#xff0c;就是我界面图片的路径设置有问题&#xff0c;在打成Jar包后就不能…

浏览器加载本地图片并预览

function imgPreview(config) {var width config.width || 200;var height config.height || 200;return {/*** ie9- 预览图片* param {type} input_file ,$(input[typefile]) 或者 $(input[typefile])[0]* param {type} callback 回掉参数为&#xff0c;$(居中的图片)* retu…

【Android】简单图片浏览器

开始重新学习Android&#xff0c;还是依照《疯狂Android讲义》。 简单图片浏览器&#xff1a; 功能&#xff1a;图片浏览器&#xff0c;点击图片可以切换。 注意&#xff1a;在res/drawable中添加p1.jpg、p2.jpg、p3.jpg。 Java代码&#xff1a; import android.support.v7.…