python tkiner实现 一个小小的图片翻页功能的示例代码
作者:论一个测试的养成 时间:2023-07-19 18:40:32
具体代码如下所示:
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()
样式如下:有点丑,不过功能没毛病,就先这么着吧~~~
点击图片展示之后
上一页下一页可以用,再次点击图片展示
来源:https://blog.csdn.net/weixin_44517891/article/details/105806530
标签:python,tkiner,翻页
0
投稿
猜你喜欢
javascript代码实现简易计算器
2024-04-16 08:57:46
vim中tagbar配置以及打字时隐藏鼠标的方法
2022-06-23 14:34:49
详解将Python程序(.py)转换为Windows可执行文件(.exe)
2022-05-29 20:46:25
MySQL大量脏数据如何只保留最新的一条(最新推荐)
2024-01-25 22:41:04
微信支付PHP SDK之微信公众号支付代码详解
2024-05-10 14:21:21
MySQL Administrator 登录报错的解决方法
2024-01-21 06:53:06
Git 教程之服务器搭建详解
2022-07-28 06:44:20
MYSQL存储过程即常用逻辑知识点总结
2024-01-21 07:36:36
浅析Python pandas模块输出每行中间省略号问题
2022-03-27 21:26:03
Python教程之类型转换详解
2021-03-23 02:48:17
Golang中接收者方法语法糖的使用方法详解
2024-05-21 10:26:49
python @property 装饰器使用方法
2021-04-01 00:38:05
python中matplotlib调整图例位置的方法实例
2023-09-11 08:52:13
Golang中channel的原理解读(推荐)
2024-02-08 15:41:38
Python工程师面试题 与Python Web相关
2021-11-10 13:00:48
MySQL中查看数据库安装路径的方法
2024-01-16 04:19:46
python实战小游戏之考验记忆力
2023-02-23 14:29:54
sqlserver2005 行列转换实现方法
2024-01-14 01:34:56
Python使用正则匹配实现抓图代码分享
2021-09-06 10:47:28
Linux下实现MySQL数据备份和恢复的命令使用全攻略
2024-01-18 11:44:46