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

 更新时间:2020年06月24日 09:58:57   作者:论一个测试的养成  
这篇文章主要介绍了python tkiner实现 一个小小的图片翻页功能,需要的朋友可以参考下

具体代码如下所示:

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中计算移动平均值的方法

    在Python中计算移动平均值的方法

    在这篇文章中,我们将看到如何在Python中计算移动平均值,移动平均是指总观测值集合中固定大小子集的一系列平均值,它也被称为滚动平均,文中通过代码示例讲解的非常详细,需要的朋友可以参考下
    2024-10-10
  • Python中检查字符串是否仅包含字母的方法详解

    Python中检查字符串是否仅包含字母的方法详解

    这篇文章主要为大家详细介绍了Python中的多种方法来检查字符串是否只由字母组成,以及它们的应用场景和优劣,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-11-11
  • Python中比较特别的除法运算和幂运算介绍

    Python中比较特别的除法运算和幂运算介绍

    这篇文章主要介绍了Python中比较特别的除法运算和幂运算介绍,“/”这个是除法运算,那么这个“//”呢?“*”这个是乘法运算,那么这个“**”呢?本文就讲解这些运算的不同,需要的朋友可以参考下
    2015-04-04
  • Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str

    Python BeautifulSoup [解决方法] TypeError: list indices must be

    这篇文章主要介绍了Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Python实现语音启动电脑应用程序

    Python实现语音启动电脑应用程序

    这篇文章主要为大家详细介绍了如何使用Python实现语音启动电脑应用程序功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一学习一下
    2025-03-03
  • python 写一个文件分发小程序

    python 写一个文件分发小程序

    这篇文章主要介绍了python 写一个文件分发小程序,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2020-12-12
  • Python标准库之Sys模块使用详解

    Python标准库之Sys模块使用详解

    这篇文章主要介绍了Python标准库之Sys模块使用详解,本文讲解了使用sys模块获得脚本的参数、处理模块、使用sys模块操作模块搜索路径、使用sys模块查找内建模块、使用sys模块查找已导入的模块等使用案例,需要的朋友可以参考下
    2015-05-05
  • python threading和multiprocessing模块基本用法实例分析

    python threading和multiprocessing模块基本用法实例分析

    这篇文章主要介绍了python threading和multiprocessing模块基本用法,结合实例形式详细分析了Python中threading和multiprocessing模块基本概念、功能、使用方法及相关操作注意事项,需要的朋友可以参考下
    2019-07-07
  • Python入门篇之文件

    Python入门篇之文件

    文件是我们储存信息的地方,我们经常要对文件进行读、写、删除等的操作,在Python中,我们可用Python提供的函数和方法方便地操作文件。文件可以通过调用open或file来打开,open通常比file更通用,因为file几乎都是为面向对象程序设计量身打造
    2014-10-10
  • Python基于xlutils修改表格内容过程解析

    Python基于xlutils修改表格内容过程解析

    这篇文章主要介绍了Python基于xlutils修改表格内容过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07

最新评论