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简单判断序列是否为空的方法
Jun 30 Python
深入解析Python中的__builtins__内建对象
Jun 21 Python
Python中str is not callable问题详解及解决办法
Feb 10 Python
Python解决八皇后问题示例
Apr 22 Python
详解利用python+opencv识别图片中的圆形(霍夫变换)
Jul 01 Python
python爬虫 线程池创建并获取文件代码实例
Sep 28 Python
vim自动补全插件YouCompleteMe(YCM)安装过程解析
Oct 21 Python
python文件操作的简单方法总结
Nov 07 Python
python实现七段数码管和倒计时效果
Nov 23 Python
python实现拉普拉斯特征图降维示例
Nov 25 Python
Python超简单容易上手的画图工具库推荐
May 10 Python
从np.random.normal()到正态分布的拟合操作
Jun 02 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
重置版游戏视频
2020/04/09 魔兽争霸
JoshChen_php新手进阶高手不可或缺的规范介绍
2013/08/16 PHP
php内核解析:PHP中的哈希表
2014/01/30 PHP
分享一段PHP制作的中文拼音首字母工具类
2014/12/11 PHP
php中namespace use用法实例分析
2016/01/22 PHP
List the UTC Time on a Computer
2007/06/11 Javascript
alixixi runcode.asp的代码不错的应用
2007/08/08 Javascript
关于document.cookie的使用javascript
2008/04/11 Javascript
一个很酷的拖动层的js类,兼容IE及Firefox
2009/06/23 Javascript
推荐17个优美新鲜的jQuery的工具提示插件
2012/09/14 Javascript
使用javascipt---实现二分查找法
2013/04/10 Javascript
javascript右下角弹层及自动隐藏(自己编写)
2013/11/20 Javascript
jquery选择器之层级过滤选择器详解
2014/01/27 Javascript
用js一次改变多个input的readonly属性值的方法
2014/06/11 Javascript
JavaScript实现简单的tab选项卡切换
2016/01/05 Javascript
JavaScript通过代码调用Flash显示的方法
2016/02/02 Javascript
getElementById().innerHTML与getElementById().value的区别
2016/10/27 Javascript
如何不用安装python就能在.NET里调用Python库
2019/07/12 Python
对django的User模型和四种扩展/重写方法小结
2019/08/17 Python
Python破解BiliBili滑块验证码的思路详解(完美避开人机识别)
2020/02/17 Python
解决windows上安装tensorflow时报错,“DLL load failed: 找不到指定的模块”的问题
2020/05/20 Python
美体小铺英国官网:The Body Shop英国
2017/01/24 全球购物
美国椅子和沙发制造商:La-Z-Boy
2020/10/25 全球购物
中专生自荐信
2013/10/12 职场文书
初中优秀教师事迹材料
2014/08/18 职场文书
党支部党的群众路线对照检查材料
2014/09/24 职场文书
乡镇计划生育工作汇报
2014/10/28 职场文书
家长对孩子的寄语
2015/02/26 职场文书
污水处理保证书
2015/05/09 职场文书
实施意见格式范本
2015/06/05 职场文书
图书借阅制度范本
2015/08/06 职场文书
网络研修心得体会
2016/01/08 职场文书
《去年的树》教学反思
2016/02/18 职场文书
幼儿园小班教学反思
2016/03/03 职场文书
tomcat默认最大连接数及相关调整方法
2022/05/06 Servers
pandas时间序列之pd.to_datetime()的实现
2022/06/16 Python