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中List的sort方法指南
Sep 01 Python
Python的pycurl包用法简介
Nov 13 Python
Python中元组,列表,字典的区别
May 21 Python
浅谈numpy中linspace的用法 (等差数列创建函数)
Jun 07 Python
Python中 传递值 和 传递引用 的区别解析
Feb 22 Python
python读取TXT每行,并存到LIST中的方法
Oct 26 Python
python微元法计算函数曲线长度的方法
Nov 08 Python
Python读取csv文件分隔符设置方法
Jan 14 Python
Pytorch 搭建分类回归神经网络并用GPU进行加速的例子
Jan 09 Python
什么是Python包的循环导入
Sep 08 Python
python 窃取摄像头照片的实现示例
Jan 08 Python
python如何查找列表中元素的位置
May 30 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/10/09 PHP
mysql5详细安装教程
2007/01/15 PHP
浅谈PHP封装CURL
2019/03/06 PHP
JS 日期验证正则附asp日期格式化函数
2009/09/11 Javascript
jQuery中实现动画效果的基本操作介绍
2013/04/16 Javascript
利用javaScript实现点击输入框弹出窗体选择信息
2013/12/11 Javascript
JavaScript函数的4种调用方法详解
2014/04/22 Javascript
javascript实现网页子页面遍历回调的方法(涉及 window.frames、递归函数、函数上下文)
2015/07/27 Javascript
js+css实现上下翻页相册代码分享
2015/08/18 Javascript
浅谈在js传递参数中含加号(+)的处理方式
2016/10/11 Javascript
vue父组件向子组件传递多个数据的实例
2018/03/01 Javascript
AngularJS实现与后台服务器进行交互的示例讲解
2018/08/13 Javascript
Fundebug支持监控微信小程序HTTP请求错误的方法
2019/02/21 Javascript
js 实现watch监听数据变化的代码
2019/10/13 Javascript
JS removeAttribute()方法实现删除元素的某个属性
2021/01/11 Javascript
深入理解Javascript中的this关键字
2015/03/27 Python
python爬虫基本知识
2018/03/05 Python
Django配置celery(非djcelery)执行异步任务和定时任务
2018/07/16 Python
Python TestCase中的断言方法介绍
2019/05/02 Python
python简单区块链模拟详解
2019/07/03 Python
Python连接Mysql进行增删改查的示例代码
2020/08/03 Python
python批量修改交换机密码的示例
2020/09/22 Python
使用HTML5和CSS3表单验证功能
2017/05/05 HTML / CSS
HTML5语义化元素你真的用对了吗
2019/08/22 HTML / CSS
英国豪华装饰照明品牌的在线零售商:Inspyer Lighting
2019/12/10 全球购物
教师实习自我鉴定
2013/12/11 职场文书
二年级数学教学反思
2014/01/21 职场文书
母亲节感恩活动记录
2014/03/16 职场文书
升国旗仪式主持词
2014/03/19 职场文书
驻村工作先进事迹
2014/08/14 职场文书
党员对照检查材料思想汇报
2014/09/16 职场文书
redis中lua脚本使用教程
2021/11/01 Redis
开机音效回归! Windows 11重新引入开机铃声
2021/11/21 数码科技
Python制作春联的示例代码
2022/01/22 Python
简单聊聊TypeScript只读修饰符
2022/04/06 Javascript
Go中使用gjson来操作JSON数据的实现
2022/08/14 Golang