2017-06-11 10 views
0

私は今、tkinter GUIプログラムを作成中です ボタンをクリックするとdef slideboldbuttondef GUI_PARTに表示されますが、私のコードではスライドショーは機能しません。tkinter:ボタンにスライドショー機能を実装しますか?

助けてください。

class mainapp(): 
    def slide(self): 
     root1=Tk() 
     self.root1.geometry("+{}+{}".format(70, 100)) 
     title("a simple Tkinter slide show") 
     # delay in seconds (time each slide shows) 
     delay = 2.5 
     imageFiles=glob.glob('/home/imagefolder/*.png') 
     photos = [PhotoImage(file=fname) for fname in imageFiles] 
     button = Button(root1,command=root1.destroy) 
     button.pack(padx=5, pady=5) 
     for photo in photos: 
      button["image"] = photo 
      root1.update() 
      time.sleep(delay) 

    def GUI_PART(self, Master): 
     self.master = Master 
     Master.title("Start") 
     self.masterFrame = Frame(self.master) 
     self.masterFrame.pack() 
     ...   
     self.boldbutton = Button(self.tool3_frame, text="Slide show",command=self.slide) 
     self.boldbutton.pack(side=LEFT) 
+0

は誰がこの問題を助けがあります? – Kmin

+0

tkinterプログラムで 'Tk()'を複数回呼び出すことは望ましくありません。代わりに['tk.TopLevel'](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/toplevel.html)ウィンドウを作成してください。また、 'mainapp'インスタンスを作成した後で' GUI_PART() 'メソッドを呼び出すようにしてください。 – martineau

答えて

0

のGUIツールキットは、イベント駆動型あり、Tkinterのも例外ではありません。

これは、プログラムがmainloop()で実行されることを意味し、スリープ付きのループを使用して画像を表示することはできません。

あなたのアプリケーションオブジェクトに画像パスのリストを保存し、after()メソッドを使用します。さらに、私はアプリケーションクラスをTkから継承させるでしょう。

例(Python2でのpython 3、Tkinter代わりtkinterの使用):

import glob 
import tkinter as tk 
from PIL import Image, ImageTk 


class ImageViewer(tk.Tk): 

    def __init__(self): 
     """Create the ImageViewer.""" 
     # You can press q to quit the program. 
     self.bind_all('q', self.do_exit) 
     # Attributes for the image handling. 
     self.image_names=glob.glob('/home/imagefolder/*.png') 
     self.index = 0 
     self.photo = None 
     # We'll use a Label to display the images. 
     self.label = tk.Label(self) 
     self.label.pack(padx=5, pady=5) 
     # Delay should be in ms. 
     self.delay = 1000*2.5 
     # Display the first image. 
     self.show_image() 

    def show_image(self): 
     """Display an image.""" 
     # We need to use PIL.Image to open png files, since 
     # tkinter's PhotoImage only reads gif and pgm/ppm files. 
     image = Image.open(self.image_names[index]) 
     # We need to keep a reference to the image! 
     self.photo = ImageTk.PhotoImage(image) 
     self.index += 1 
     if self.index == len(self.image_names): 
      self.index = 0 
     # Set the image 
     self.label['image'] = self.photo 
     # Tell tkinter we want this method to be called again after a delay. 
     self.after(self.delay, next_image) 

    def do_exit(self, event): 
     """ 
     Callback to handle quitting. 

     This is necessary since the quit method does not take arguments. 
     """ 
     self.quit() 

root = ImageViewer() 
root.mainloop() 
+0

素晴らしい!ご協力いただきありがとうございます。 – Kmin

関連する問題