python tkiner实现 一个小小的图片翻页功能的示例代码


Posted in Python onJune 24, 2020

具体代码如下所示:

import tkinter as tk
import tkinter.messagebox
import copy
import os,sys
def get_picture(dirs):
'''获得所有图片'''
  picture_list = []
  for dir,dir_abs,files in os.walk(dirs):
    for file in files:
      if file.endswith('.gif'):
        picture_list.append(os.path.join(dir,file))
  return picture_list
class Window:
  button_list = []
  object_list = []
  pictures = get_picture(picture_path)
  file = pictures[0]
  is_show = True
  index = 0
  image_file = ''
  def __init__(self):
    '''创建窗口和frame'''
    self.window = tk.Tk()
    self.window.title('my window')
    self.window.geometry('600x600')
    self.frame = tk.Frame(self.window)
    self.frame.pack()
    self.frame_l = tk.Frame(self.frame)
    self.frame_r = tk.Frame(self.frame)
    self.frame_l.pack(side='left')
    self.frame_r.pack(side='right')
    self.frame_ll = tk.Frame(self.frame_r)
    self.frame_rr = tk.Frame(self.frame_r)
    self.frame_ll.pack(side='left')
    self.frame_rr.pack(side='right')
    
  def next_picture(self):
    '''下一张图片'''
    self.index = self.pictures.index(self.file)
    self.index += 1
    if self.index < len(self.pictures):
      self.checkout_button()
      self.file = self.pictures[self.index]
      self.create_canvas(self.file)
    else:
      self.index = len(self.pictures) - 1
      tkinter.messagebox.showinfo('提示', '已近是最后一张了')

  def checkout_button(self):
    '''判断列表中是否只有button对象'''
    object_list_copy = copy.copy(self.object_list)
    for ob in self.object_list:
      if ob in self.button_list:
        pass
      else:
        b = object_list_copy.pop(self.object_list.index(ob))
        b.destroy()
    self.object_list = object_list_copy

  def pre_picture(self):
    '''上一页'''
    self.index = self.pictures.index(self.file)
    self.index -= 1
    if self.index >= 0:
      self.checkout_button()
      self.file = self.pictures[self.index]
      self.create_canvas(self.file)
    else:
      self.index = 0
      tkinter.messagebox.showinfo('提示', '已经是第一张了')

  def show_picture(self):
    '''展示图片和翻页按钮'''
    self.file = self.pictures[0]
    if self.is_show:
      self.is_show = False
      self.create_canvas(self.file)
      button1 = tk.Button(self.frame_ll, text='上一张', width=10, height=1, command=self.pre_picture)
      button1.pack()
      button2 = tk.Button(self.frame_rr, text='下一张', width=10, height=1, command=self.next_picture)
      button2.pack()
      self.button_list.append(button1)
      self.button_list.append(button2)
      self.object_list.extend(self.button_list)
    else:
      self.is_show = True
      while self.object_list:
        o = self.object_list.pop()
        o.destroy()
  def new_button(self):
    '''创建展示按钮'''
    tk.Button(self.frame_l, text='图片展示', width=10, height=1, command=self.show_picture).pack()

  def create_canvas(self,file):
    '''用画布展示图片'''
    self.image_file = tk.PhotoImage(file=file)
    canvas = tk.Canvas(self.frame_r, height=500, width=600)
    canvas.create_image(1, 1, anchor='nw', image=self.image_file)
    canvas.pack()
    self.object_list.append(canvas)

  def run(self):
    '''主程序调用'''
    self.window.mainloop()

if __name__ == '__main__':
  w = Window()
  w.new_button()
  w.run()

样式如下:有点丑,不过功能没毛病,就先这么着吧~~~

python tkiner实现 一个小小的图片翻页功能的示例代码

点击图片展示之后

python tkiner实现 一个小小的图片翻页功能的示例代码

上一页下一页可以用,再次点击图片展示

python tkiner实现 一个小小的图片翻页功能的示例代码

总结

到此这篇关于python tkiner实现 一个小小的图片翻页功能的文章就介绍到这了,更多相关python tkiner实现图片翻页内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python设计模式之单例模式实例
Apr 26 Python
Linux下使用python调用top命令获得CPU利用率
Mar 10 Python
利用Django框架中select_related和prefetch_related函数对数据库查询优化
Apr 01 Python
wxPython实现窗口用图片做背景
Apr 25 Python
Python中作用域的深入讲解
Dec 10 Python
Python实现从SQL型数据库读写dataframe型数据的方法【基于pandas】
Mar 18 Python
python读写配置文件操作示例
Jul 03 Python
python各类经纬度转换的实例代码
Aug 08 Python
python画微信表情符的实例代码
Oct 09 Python
Python读取Excel一列并计算所有对象出现次数的方法
Sep 04 Python
实现Python3数组旋转的3种算法实例
Sep 16 Python
Pyqt5将多个类组合在一个界面显示的完整示例
Sep 04 Python
在tensorflow实现直接读取网络的参数(weight and bias)的值
Jun 24 #Python
基于pytorch中的Sequential用法说明
Jun 24 #Python
django haystack实现全文检索的示例代码
Jun 24 #Python
Python爬虫如何应对Cloudflare邮箱加密
Jun 24 #Python
python使用自定义钉钉机器人的示例代码
Jun 24 #Python
pytorch中的weight-initilzation用法
Jun 24 #Python
pytorch查看模型weight与grad方式
Jun 24 #Python
You might like
一贴学会PHP 新手入门教程
2009/08/03 PHP
php smarty截取中文字符乱码问题?gb2312/utf-8
2011/11/07 PHP
php实现多张图片上传加水印技巧
2013/04/18 PHP
使用php判断服务器是否支持Gzip压缩功能
2013/09/24 PHP
php的XML文件解释类应用实例
2014/09/22 PHP
Yii实现多按钮保存与提交的方法
2014/12/03 PHP
thinkphp3.2点击刷新生成验证码
2016/02/16 PHP
一些常用的JS功能函数代码
2009/06/23 Javascript
URL地址中的#符号使用说明
2011/02/12 Javascript
JQuery中判断一个元素下面是否有内容或者有某个标签的判断代码
2012/02/02 Javascript
JS实现淘宝支付宝网站的控制台菜单效果
2015/09/28 Javascript
jQuery模拟窗口抖动效果
2017/03/15 Javascript
Vue工程模板文件 webpack打包配置方法
2017/12/26 Javascript
ReactNative实现Toast的示例
2017/12/31 Javascript
深入理解webpack process.env.NODE_ENV配置
2020/02/23 Javascript
vue使用screenfull插件实现全屏功能
2020/09/17 Javascript
[38:54]完美世界DOTA2联赛PWL S2 Rebirth vs LBZS 第一场 11.28
2020/12/01 DOTA
python访问纯真IP数据库的代码
2011/05/19 Python
利用Django框架中select_related和prefetch_related函数对数据库查询优化
2015/04/01 Python
Python 关于反射和类的特殊成员方法
2017/09/14 Python
Python中执行存储过程及获取存储过程返回值的方法
2017/10/07 Python
python selenium爬取斗鱼所有直播房间信息过程详解
2019/08/09 Python
python单向链表的基本实现与使用方法【定义、遍历、添加、删除、查找等】
2019/10/24 Python
Python安装tar.gz格式文件方法详解
2020/01/19 Python
检测浏览器是否支持html5视频的代码
2013/03/28 HTML / CSS
AmazeUI 按钮交互的实现示例
2020/08/24 HTML / CSS
应届生求职自荐信范文
2014/04/07 职场文书
企业年度评优方案
2014/06/02 职场文书
服务理念标语
2014/06/18 职场文书
大四毕业生自荐书
2014/07/05 职场文书
运动会演讲稿50字
2014/08/25 职场文书
钱塘江大潮导游词
2015/02/03 职场文书
入党积极分子培养人意见
2015/06/02 职场文书
OpenCV3.3+Python3.6实现图片高斯模糊
2021/05/18 Python
Java基础-封装和继承
2021/07/02 Java/Android
修改Nginx配置返回指定content-type的方法
2022/09/23 Servers