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之Python文档
Oct 10 Python
Python将xml和xsl转换为html的方法
Mar 10 Python
Python多线程编程(七):使用Condition实现复杂同步
Apr 05 Python
详解Python中time()方法的使用的教程
May 22 Python
Python 字符串大小写转换的简单实例
Jan 21 Python
Python实现改变与矩形橡胶的线条的颜色代码示例
Jan 05 Python
提升Python程序性能的7个习惯
Apr 14 Python
Python pandas实现excel工作表合并功能详解
Aug 29 Python
python3.x 生成3维随机数组实例
Nov 28 Python
python能自学吗
Jun 18 Python
基于Python实现体育彩票选号器功能代码实例
Sep 16 Python
Python中的matplotlib绘制百分比堆叠柱状图,并为每一个类别设置不同的填充图案
Apr 20 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
分享PHP入门的学习方法
2007/01/02 PHP
php heredoc和phpwind的模板技术使用方法小结
2008/03/28 PHP
7个超级实用的PHP代码片段
2011/07/11 PHP
js实现页面打印功能实例代码(附去页眉页脚功能代码)
2009/12/15 Javascript
浅析JavaScript原型继承的陷阱
2013/12/03 Javascript
jQuery实现预加载图片的方法
2015/03/17 Javascript
分享五个有用的jquery小技巧
2015/10/08 Javascript
AngularJS控制器详解及示例代码
2016/08/16 Javascript
详解Angular.js的$q.defer()服务异步处理
2016/11/06 Javascript
jquery插件bootstrapValidator表单验证详解
2016/12/15 Javascript
vue-cli构建项目使用 less的方法
2017/10/04 Javascript
Vue的elementUI实现自定义主题方法
2018/02/23 Javascript
nodejs实现超简单生成二维码的方法
2018/03/17 NodeJs
vue项目引入ts步骤(小结)
2019/10/31 Javascript
序列化模块json代码实例详解
2020/03/03 Javascript
微信小程序清空输入框信息与实现屏幕往上滚动的示例代码
2020/06/23 Javascript
Python实现代码统计工具(终极篇)
2016/07/04 Python
Python基于列表模拟堆栈和队列功能示例
2018/01/05 Python
python+matplotlib实现动态绘制图片实例代码(交互式绘图)
2018/01/20 Python
NumPy排序的实现
2020/01/21 Python
斯洛伐克时尚服装网上商店:Cellbes
2016/10/20 全球购物
Ootori在线按摩椅店:一家专业的按摩椅制造商
2019/04/10 全球购物
应届毕业生求职信范文分享
2013/12/26 职场文书
电脑销售顾问自荐信
2014/01/29 职场文书
工厂保安员岗位职责
2014/01/31 职场文书
《夏夜多美》教学反思
2014/02/17 职场文书
护理专科毕业自荐信范文
2014/04/21 职场文书
道德演讲稿
2014/05/21 职场文书
新闻学专业职业生涯规划范文:我的人生我做主
2014/09/12 职场文书
干部作风整顿个人剖析材料
2014/10/06 职场文书
幼儿园感恩节活动方案
2014/10/06 职场文书
安全员岗位职责
2015/02/10 职场文书
严以用权专题学习研讨会发言材料
2015/11/09 职场文书
经典励志格言:每日一句,让你每天充满能量
2019/08/16 职场文书
MySQL令人咋舌的隐式转换
2021/04/05 MySQL