2016-12-26 43 views
0

我在Tkinter中創建了一個嚮導。幾乎每個步驟都應該使用導航和取消按鈕。我怎樣才能做到這一點?我應該創建一個框架?一般來說,所有的步驟是否應該創建爲不同的框架?在Tkinter中創建嚮導

+0

真不明白,你能告訴更多的你的嚮導是如何組織和接口的樣子,也究竟是頁腳中的作用?按鈕的功能似乎已經顯而易見了。 –

+1

請參見[在tkinter中的兩個幀之間切換](http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter)。它使用'Frame'來創建'Pages' - 你可以以相同的方式使用Frame或者使用Frame來創建帶有按鈕的widget(並且放置其他元素),然後使用這個widget來構建Pages。 – furas

回答

1

這個問題的答案與Switch between two frames in tkinter沒有多大區別。唯一顯着的區別是你想在底部有一組永久的按鈕,但是沒有什麼特別的做法 - 只需創建一個框架,並將一些按鈕作爲包含單獨頁面(或步驟)的小部件的兄弟。

我建議爲每個嚮導步驟創建一個單獨的類,它繼承自Frame。然後,只需要移除當前步驟的幀並顯示下一步的幀。

例如,步驟可能看起來像這樣(使用python 3語法):

class Step1(tk.Frame): 
    def __init__(self, parent): 
     super().__init__(parent) 

     header = tk.Label(self, text="This is step 1", bd=2, relief="groove") 
     header.pack(side="top", fill="x") 

     <other widgets go here> 

其他步驟將是在概念上相同:框架與一羣小部件。

您的主程序或您的Wizard類將根據需要實例化每個步驟,或提前實例化它們。然後,您可以編寫一個方法,將步驟編號作爲參數並相應地調整UI。

例如:

class Wizard(tk.Frame): 
    def __init__(self, parent): 
     super().__init__(parent) 

     self.current_step = None 
     self.steps = [Step1(self), Step2(self), Step3(self)] 

     self.button_frame = tk.Frame(self, bd=1, relief="raised") 
     self.content_frame = tk.Frame(self) 

     self.back_button = tk.Button(self.button_frame, text="<< Back", command=self.back) 
     self.next_button = tk.Button(self.button_frame, text="Next >>", command=self.next) 
     self.finish_button = tk.Button(self.button_frame, text="Finish", command=self.finish) 

     self.button_frame.pack(side="bottom", fill="x") 
     self.content_frame.pack(side="top", fill="both", expand=True) 

     self.show_step(0) 

    def show_step(self, step): 

     if self.current_step is not None: 
      # remove current step 
      current_step = self.steps[self.current_step] 
      current_step.pack_forget() 

     self.current_step = step 

     new_step = self.steps[step] 
     new_step.pack(fill="both", expand=True) 

     if step == 0: 
      # first step 
      self.back_button.pack_forget() 
      self.next_button.pack(side="right") 
      self.finish_button.pack_forget() 

     elif step == len(self.steps)-1: 
      # last step 
      self.back_button.pack(side="left") 
      self.next_button.pack_forget() 
      self.finish_button.pack(side="right") 

     else: 
      # all other steps 
      self.back_button.pack(side="left") 
      self.next_button.pack(side="right") 
      self.finish_button.pack_forget() 

的功能nextback的定義,和finish是非常直接的:只需要調用self.show_step(x)其中x是應該示出的步驟的數量。例如,next可能是這樣的:

def next(self): 
    self.show_step(self.current_step + 1) 
+0

謝謝,1)腳註,幾乎所有的步驟應該分享? 2)我應該在哪裏創建'root = Tk()'? – Jodooomi

+0

@Jodooomi:「footer」是'self.button_frame'。你可以把任何你想要的東西放在那裏,或者創建多個頁腳或標題或任何你想要的東西。無論何時何地,您都可以像通常那樣創建根窗口。 –

+0

是的,我應該在哪裏創建根,你能告訴我嗎?在「主」? – Jodooomi