python tkiner實現(xiàn) 一個小小的圖片翻頁功能的示例代碼
具體代碼如下所示:
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):
'''創(chuàng)建窗口和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('提示', '已經(jīng)是第一張了')
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):
'''創(chuàng)建展示按鈕'''
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):
'''主程序調(diào)用'''
self.window.mainloop()
if __name__ == '__main__':
w = Window()
w.new_button()
w.run()
樣式如下:有點丑,不過功能沒毛病,就先這么著吧~~~

點擊圖片展示之后

上一頁下一頁可以用,再次點擊圖片展示

總結(jié)
到此這篇關(guān)于python tkiner實現(xiàn) 一個小小的圖片翻頁功能的文章就介紹到這了,更多相關(guān)python tkiner實現(xiàn)圖片翻頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)PPT/PPTX批量轉(zhuǎn)換成PDF
這篇文章主要為大家詳細介紹了如何使用Python將PowerPoint演示文稿(PPT、PPTX等)轉(zhuǎn)換為PDF文件,使演示內(nèi)容能夠在更多的設(shè)備上展示,感興趣的小伙伴可以了解下2024-01-01
Python提取特定時間段內(nèi)數(shù)據(jù)的方法實例
今天小編就為大家分享一篇關(guān)于Python提取特定時間段內(nèi)數(shù)據(jù)的方法實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04
Jupyter notebook 更改文件打開的默認路徑操作
這篇文章主要介紹了Jupyter notebook 更改文件打開的默認路徑操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05
python數(shù)據(jù)結(jié)構(gòu)之遞歸方法講解
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之遞歸講解,遞歸是解決問題的一種方法,它將問題不斷地分成更小的子問題,直到子問題可以用普通的方法解決。通常情況下,遞歸會使用一個不停調(diào)用自己的函數(shù),下面來看看文章對此的詳細介紹吧2021-12-12
使用Django搭建網(wǎng)站實現(xiàn)商品分頁功能
這篇文章主要介紹了使用Django搭建網(wǎng)站實現(xiàn)商品分頁功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
超詳細注釋之OpenCV Haar級聯(lián)檢測器進行面部檢測
這篇文章主要介紹了OpenCV Haar級聯(lián)檢測器進行面部檢測,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

