2016-12-26 26 views
0

私はTkinterでウィザードを作成しています。ほとんどの各ステップでは、ナビゲーションとキャンセルのためのボタンと同じフッターがあります。どうすればこれを達成できますか?フレームを作成する必要がありますか?そして、一般に、すべてのステップを異なるフレームとして作成する必要がありますか?Tkinterでウィザードを作成する

+0

本当に明確ではない、あなたは少しあなたのウィザードが構造化されてどのように多くの、何のインターフェイスの外観を伝えることができ、また、フッターの役割は、正確には何ですか?ボタンの機能は、タイトルによって既に明らかです。 –

+1

[tkinterの2つのフレームを切り替える](http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter)を参照してください。それは 'Frame'を使って' Pages'を作成します - 同じ方法で 'Frame'を使うか、' Frame'を使ってボタン付きのウィジェットを作り(そして他の要素のために置く)、このウィジェットを使って '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> 

他のステップは、概念的には同一である:ウィジェットの束を持つフレーム。

メインプログラムまたはウィザードクラスは、必要に応じて各ステップをインスタンス化するか、あらかじめそれらをすべてインスタンス化します。次に、ステップ番号をパラメータとし、それに応じて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は非常に単純である:xが示さなければならないステップの数であるだけself.show_step(x)を呼び出します。例えば、nextは次のようになります。

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

ありがとうございます、1)フッタは、ほぼすべてのステップを共有する必要がありますか? 2) 'root = Tk()'はどこで作るべきですか? – Jodooomi

+0

@ Jodooomi: "フッター"は 'self.button_frame'です。必要なものを置いたり、複数のフッターやヘッダーなどを作成することができます。通常どおり/いつでもどこでもルートウィンドウを作成できます。 –

+0

ええ、どこにルートを作成すればいいですか、私に見せてもらえますか? 「メイン」に? – Jodooomi

関連する問題