2017-12-18 11 views
0

カタログのfiles.gifのリストから写真を見たいと思っています。写真は3秒ごとに変更されるはずです。 私は別の解決策を試しましたが、いずれかの写真は写真のみで表示されます。ファイル一覧から写真を表示

TIA

from tkinter import * 
import os 
path = os.getcwd() 
arr = [] 
for files in next(os.walk('/home/vimart/Python/img/'))[2]: 
     arr.append('/home/vimart/Python/img' + "/" + files) 
canvas_width = 300 
canvas_height =300 

master = Tk() 

canvas = Canvas(master, 
      width=canvas_width, 
      height=canvas_height) 
canvas.pack() 

def display(): 
     canvas.create_image(20,20, anchor=NW, image=canvas.img) 


def get_picture(): 
     for picture in arr: 
       canvas.img = PhotoImage(picture) 
       master.after(3000, display) 
get_picture() 

mainloop() 
+0

googleの「tkinter slideshow」には、これを行う方法の例が多数あります。 – Novel

+0

'for'は使用しません。一度にすべての画像を表示し、最後の画像のみを表示します。 1つのイメージのみを表示する関数を実行するには 'after()'を使います。グローバル変数を使用してどのイメージを記憶し、 'next_image + = 1'、' picture = arr [next_image] 'を実行します。 – furas

+0

類似の画像ビューアについては、[この回答](https://stackoverflow.com/a/47869161/7032856)を参照してください。 – Nae

答えて

2

私はそれが説明を必要としないと思います。

import tkinter as tk 
from PIL import ImageTk 
import os 

# --- functions --- 

def get_filenames(path): 
    result = [] 

    #for one_file in os.listdir(path): 
    for one_file in next(os.walk(path))[2]: 
     if one_file.lower().endswith('.gif'): # sugessted by Nae 
      result.append(path + one_file) 

    return result 

def display(): 
    global current_index 

    picture = arr[current_index] 
    canvas.img = ImageTk.PhotoImage(file=picture) 
    canvas.create_image(20,20, anchor='nw', image=canvas.img) 

    current_index = (current_index + 1) % len(arr) 

    master.after(3000, display) 

# --- main --- 

path = '/home/vimart/Python/img/' 

arr = get_filenames(path) 
current_index = 0 

canvas_width = 300 
canvas_height = 300 

master = tk.Tk() 

canvas = tk.Canvas(master, width=canvas_width, height=canvas_height) 
canvas.pack() 

display() 

master.mainloop() 
+0

ファイルタイプフィルタリングを追加するには、if one_file.lower().endswith( '。gif'): 'を追加する前に追加します。 – Nae

+1

@Naeあなたは正しいです。私は 'os.listdir'を追加するとこれについて考えていましたが、最後にこれをスキップします。これが追加されました。 – furas

関連する問題