python翻页_如何用python tkiner实现图片翻页功能
如何用python tkiner实现图片翻页功能
发布时间:2020-06-26 12:52:59
来源:亿速云
阅读:108
作者:Leah
如何用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翻页_如何用python tkiner实现图片翻页功能相关推荐
- python小助手_如何用python写个人专属群聊提醒小助手?
前言 大家还记得教会父母玩微信是什么时候吗?父母学会后,我们的生活就发生了「质」的变化,父母也许会吐槽你的微信头像不好,要你换一个头像. 最近 pk哥 又被母后大人吐槽了,原因是亲戚微信群里某个亲戚生 ...
- python怎么读_如何用Python读写文件
前面我们已经介绍了很多Python相关的基础知识,大家是不是对Python已经有了进一步认识了呢?作为人工智能时代的热门编程语言,开始接触并学习Python的孩子越来越多,家长们都不想让自己的孩子落于 ...
- python旅游推荐系统_如何用Python搭建一个简单的推荐系统?
推荐系统的相关知识我们已在前文中提到,在这篇文章中,我们会介绍如何用Python来搭建一个简单的推荐系统. 本文使用的数据集是MovieLens数据集,该数据集由明尼苏达大学的Grouplens研究小 ...
- 用python开启相机_如何用Python打开realsenseD435相机并获取相机参数
如何用Python打开realsenseD435相机 import pyrealsense2 as rs import numpy as np import cv2 if __name__ == &q ...
- python读取二进制文件_如何用python读取二进制文件?
我发现用Python读取二进制文件特别困难.你能帮我一把吗?我需要读取这个文件,在Fortran 90中,该文件很容易被int*4 n_particles, n_groups real*4 group ...
- python md5加密_如何用python“优雅”的调用有道翻译?
前言 其实在以前就盯上有道翻译了的,但是由于时间问题一直没有研究(我的骚操作还在后面,记得关注),本文主要讲解如何用python调用有道翻译,讲解这个爬虫与有道翻译的js"斗争"的 ...
- python求平均值_如何用python求平均值
学习了Python相关数据类型,函数的知识后,利用字符串的分割实现了输入任意多个数据,并计算其平均值的小程序.思路是接收输入的字符串,以空格为分隔符,将分割的数据存入列表(lst1)中,将lst1中的 ...
- 如何用python计算圆周率_如何用python计算圆周率?
如何用python计算圆周率? python计算圆周率的方法: 圆周率没有精确的计算公式,所以只能用近似的方式计算它的近似值. 我们运用蒙特卡罗方法,思路很简单,在下面图形中随机抛置大量的点, 计算落 ...
- python做网站开发_如何用Python做网站开发
很多人想要用Python做网站开发,但是又不知从何入手,接下来小编介绍用Python做网站开发需要学习哪些知识,又应该以什么样的顺序来学. 第一步:HTML(超文本标记语言)是网页的核心,学好HTML ...
最新文章
- web前端技术分享:es6展开运算符概念和使用!
- Unity教程之-UGUI美术字体的制作与使用
- Interactive Reflection Editing (SIGGRAPH ASIA 09)
- Linux 自带的LED 灯驱动实验
- SpringBoot2 集成xJar插件 动态解密jar包,避免源码泄露或反编译
- oracle先的lvm分配,LVM实战案例之LVM空间缩减(本次对PV,VG,LV等进行空间缩减)
- 服务器管理器添加php,为web服务器添加php的支持
- Oracle asm aix盘,AIX添加ASM的裸盘,存储底层硬盘迁移
- bc8-android导航,路畅A6导航刷机固件 4.09 CN-A6-GBDS-BC8-VIN-256-V1.51
- 微型计算机原理考试试卷,微机原理试题集题库带答案
- tensorflow keras 构建神经网络、Alex net、VGG、CNN网络
- Domain Adaptation论文合集
- Microsoft SQL Server 生成随机数字、字符串、日期、验证码以及 UUID
- 弱口令介绍及破解方式
- idea中用rest风格发送delete,put请求报405(tomcat8)
- Redis命令INCRBY和INCR区别
- web期末作业设计网页——_传统节日--端午节(9页)主题节日网页
- 4月热搜:揭秘金融级人脸实名认证解决方案背后的技术硬货
- 如何让AutoHotkey在大部分游戏中好用
- MATLAB----绘制三维曲线