如何用pythontkiner實現(xiàn)圖片翻頁功能-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)萬年,十余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

如何用python tkiner實現(xiàn)圖片翻頁功能?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

具體代碼如下所示:

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()

網(wǎng)頁題目:如何用pythontkiner實現(xiàn)圖片翻頁功能-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://muchs.cn/article4/pojoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、全網(wǎng)營銷推廣、用戶體驗、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計