2016-02-11 57 views
3

每個操作系統都有一個活動指示器。 OS X和iOS有一朵點亮並以圓形圖案淡化每個踏板的花朵。 Windows有一個旋轉藍盤的東西。 Android有灰色的東西(我認爲它也可以是多種其他顏色 - IDK,我沒有太多使用Android。)Tkinter的活動指標?

在Tkinter中使用這些圖標的最佳方式是什麼?有一些內置的小部件提供了這個嗎?是否可能有一個變量,我可以指向一個圖像小部件來讓它顯示這個圖標(並對它進行動畫處理?)

我知道Tkinter提供了一個Progress Bar,它有一個不確定的模式。我不想使用它 - 我需要一些適合小方形區域,而不是長方形區域的東西。第一段描述的活動指標將是完美的。

或者是我最好的選擇,只是滾動我自己的畫布動畫?

+0

沒有答案,但也許這可以幫助MacOS https://gist.github.com/pudquick/f7cefd0d4320e0b648da –

+0

可能你將不得不使用'Canvas'或'Label'來顯示圖像,但是你必須自己動畫(使用'root.after') – furas

+0

@NelelSegura - Huh。這依賴於'AppKit' /'Cocoa'。有沒有一種方法可以將'AppKit'的小部件與'Tkinter'的小部件混合?我的印象你不能。 – ArtOfWarfare

回答

0

這應該足夠你想要做的事情。我會創建我自己的動畫或從互聯網框架下載你想要顯示的框架。畫布更多與用戶互動,這就是爲什麼我使用標籤來顯示圖像...

import tkinter as tk 

class Activity(tk.Label): 
    def __init__(self, master = None, delay = 1000, cnf = {}, **kw): 
     self._taskID = None 
     self.delay = delay 
     return super().__init__(master, cnf, **kw) 

    # starts the animation 
    def start(self): 
     self.after(0, self._loop) 

    # calls its self after a specific <delay> 
    def _loop(self): 
     currentText = self["text"] + "." 

     if currentText == "....": 
      currentText = "" 

     self["text"] = currentText 

     self._taskID = self.after(self.delay, self._loop) 

    # stopps the loop method calling its self 
    def stop(self): 
     self.after_cancel(self._taskID) 
     # Depends if you want to destroy the widget after the loop has stopped 
     self.destroy() 


class AcitivityImage(Activity): 
    def __init__(self, imagename, frames, filetype, master = None, delay = 1000, cnf = {}, **kw): 
     self._frames = [] 
     self._index = 0 
     for i in range(frames): 
      self._frames.append(tk.PhotoImage(file = imagename+str(i)+'.'+str(filetype))) 

     return super().__init__(master, delay, cnf, **kw) 

    def _loop(self): 
     self["image"] = self._frames[self._index] 

     # add one to index if the index is less then the amount of frames 
     self._index = (self._index + 1)% len(self._frames) 

     self._taskID = self.after(self.delay, self._loop) 


root = tk.Tk() 
root.geometry("500x500") 

# create a activity image widget 
#root.b = AcitivityImage("activity", 3, "png", root) 
#root.b.pack() 

# create the activity widget 
root.a = Activity(root, 500, bg = "yellow") 
root.a.pack() 

# start the loop 
root.a.start() 
#root.b.start() 

# stop the activity loop after 7 seconds 
root.after(7000, root.a.stop) 
#root.after(8000, root.b.stop) 

root.mainloop().