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 03 Python
Python语言的面相对象编程方式初步学习
Mar 12 Python
基于python的字节编译详解
Sep 20 Python
Python3实现发送QQ邮件功能(附件)
Dec 23 Python
Python实现的微信好友数据分析功能示例
Jun 21 Python
python实现微信自动回复及批量添加好友功能
Jul 03 Python
python实现ip代理池功能示例
Jul 05 Python
新手入门Python编程的8个实用建议
Jul 12 Python
Python中的单下划线和双下划线使用场景详解
Sep 09 Python
解决Keras 与 Tensorflow 版本之间的兼容性问题
Feb 07 Python
Python求凸包及多边形面积教程
Apr 12 Python
python写文件时覆盖原来的实例方法
Jul 22 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
第六节--访问属性和方法
2006/11/16 PHP
php像数组一样存取和修改字符串字符
2014/03/21 PHP
让CodeIgniter数据库缓存自动过期的处理的方法
2014/06/12 PHP
Ajax+PHP实现的模拟进度条功能示例
2019/02/11 PHP
国外Lightbox v2.03.3 最新版 下载
2007/10/17 Javascript
JS location几个方法小姐
2008/07/09 Javascript
javascript 树控件 比较好用
2009/06/11 Javascript
JavaScript对象链式操作代码(jquery)
2010/07/04 Javascript
JS trim去空格的最佳实践
2011/10/30 Javascript
文本域光标操作的jQuery扩展分享
2014/03/10 Javascript
jQuery中parents()和parent()的区别分析
2014/10/28 Javascript
JavaScript中window.showModalDialog()用法详解
2014/12/18 Javascript
jQuery仿360导航页图标拖动排序效果代码分享
2015/08/24 Javascript
jquery+ajax实现注册实时验证实例详解
2015/12/08 Javascript
nodejs获取微信小程序带参数二维码实现代码
2017/04/12 NodeJs
vue中实现移动端的scroll滚动方法
2018/03/03 Javascript
微信小程序wepy框架笔记小结
2018/08/08 Javascript
微信小程序中使用wxss加载图片并实现动画效果
2018/08/13 Javascript
Vue面试题及Vue知识点整理
2018/10/07 Javascript
二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
2013/12/03 Python
Python数组条件过滤filter函数使用示例
2014/07/22 Python
Python实现栈的方法
2015/05/26 Python
Django验证码的生成与使用示例
2017/05/20 Python
Django中的forms组件实例详解
2018/11/08 Python
Python3对称加密算法AES、DES3实例详解
2018/12/06 Python
浅析Python 读取图像文件的性能对比
2019/03/07 Python
Django windows使用Apache实现部署流程解析
2020/10/12 Python
Python如何telnet到网络设备
2021/02/18 Python
科颜氏美国官网:Kiehl’s美国
2017/01/31 全球购物
应届生妇产科护士求职信
2013/10/27 职场文书
模特职业生涯规划范文
2014/02/26 职场文书
小学一年级评语大全
2014/04/22 职场文书
机关党员四风问题个人整改措施
2014/10/26 职场文书
企业办公室主任岗位职责
2015/04/01 职场文书
matplotlib之pyplot模块实现添加子图subplot的使用
2021/04/25 Python
Spring Boot 启动、停止、重启、状态脚本
2021/06/26 Java/Android