神剑山庄资源网 Design By www.hcban.com

具体代码如下所示:

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图片翻页

神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com

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

暂无python tkiner实现 一个小小的图片翻页功能的示例代码的评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。