2016-04-19 16 views
0

私のリアクションゲーム用のユーザーインターフェイスを構築しようとしています。お互いの上にフレームを設定しましたが、ストップウォッチ機能をフレームの1つに追加すると、開始ボタンが機能しません。私は、リセットボタンと終了ボタンをテストし、正常に動作します。ストップウォッチが始まらないので、一時停止ボタンをテストすることはできません。誰でも助けてくれますか?ここでPython Tkinterストップウォッチがフレーム上で動作しません

は、フレームとストップウォッチのためのコードである:ここで

class LvlOneR(tk.Frame): 
def __init__(self, parent, controller): 
    tk.Frame.__init__(self, parent) 
    self.controller = controller 
    label = tk.Label(self, text="Level One", 
        background='white', 
        foreground='dodger blue', 
        font=TITLE_FONT) 
    label.pack(side="top", fill="x", pady=10) 
    #stopwatch 
    def update_timeText(): 
     if (state): 
      global timer 
      # Every time this function is called, 
      # we will increment 1 centisecond (1/100 of a second) 
      timer[2] += 1 
      # Every 100 centisecond is equal to 1 second 
      if (timer[2] >= 100): 
       timer[2] = 0 
       timer[1] += 1 
      # Every 60 seconds is equal to 1 min 
      if (timer[1] >= 60): 
       timer[0] += 1 
       timer[1] = 0 
      # We create our time string here 
      timeString = pattern.format(timer[0], timer[1], timer[2]) 
      # Update the timeText Label box with the current time 
      timeText.configure(text=timeString) 
      # Call the update_timeText() function after 1 centisecond 
     self.after(10, update_timeText) 
    # To start game 
    def start(): 
     global state 
     state = True 

    # To pause the game 
    def pause(): 
     global state 
     state = False 

    # To reset the timer to 00:00:00 
    def reset(): 
     global timer 
     timer = [0, 0, 0] 
     timeText.configure(text='00:00:00') 

    # To leave game our program 

    def leave(): 
     controller.show_frame("ReactionGame") 

    # Simple status flag 
    # False mean the timer is not running 
    # True means the timer is running (counting) 
    state = False 

    # Our time structure [min, sec, centsec] 
    timer = [0, 0, 0] 
    # The format is padding all the 
    pattern = '{0:02d}:{1:02d}:{2:02d}' 
    # Create a timeText Label (a text box) 
    timeText = tk.Label(self, text="00:00:00", 
         background='white', 
         foreground='dodger blue', 
         font=("Helvetica", 150)) 

    startButton = tk.Button(self, text='Start', command=start) 


    pauseButton = tk.Button(self, text='Pause', command=pause) 

    resetButton = tk.Button(self, text='Reset', command=reset) 

    quitButton = tk.Button(self, text='Quit', command=leave) 

    timeText.pack() 
    startButton.pack() 
    pauseButton.pack() 
    resetButton.pack() 
    quitButton.pack() 
    update_timeText() 

は、フレームのセットアップのためのコードです:

class Game(tk.Tk): 

def __init__(self, *args, **kwargs): 
    tk.Tk.__init__(self, *args, **kwargs) 

    # the container is where we'll stack a bunch of frames 
    # on top of each other, then the one we want visible 
    # will be raised above the others 
    container = tk.Frame(self) 
    container.pack(side="top", fill="both", expand=True) 
    container.grid_rowconfigure(0, weight=1) 
    container.grid_columnconfigure(0, weight=1) 


    self.frames = {} 
    for F in (LoginScreen, MainMenu, ReactionGame, MemoryGame, HighScores, LvlOneR): 
     page_name = F.__name__ 
     frame = F(container, self) 
     self.frames[page_name] = frame 

     # put all of the pages in the same location; 
     # the one on the top of the stacking order 
     # will be the one that is visible. 
     frame.grid(row=0, column=0, sticky="nsew") 
     frame.configure(background='white') 

    self.show_frame("LoginScreen") 

def show_frame(self, page_name): 
    '''Show a frame for the given page name''' 
    frame = self.frames[page_name] 
    frame.tkraise()     

答えて

0

Minimal, Complete and Verifiable Exampleを作成する方法をお読みください。

あなたのコードを分解しようとしなかったと思いますが

  • ウィジェット自体は機能しますか?
  • いつエラーが表示され始めますか? (あるレベルのウィジェットを他の人の中に置いていますか?)

それはデバッグについてです。コードを単一の要素に分解し、それらを1つずつ検証します。

私にとって、「これはコードですが、動作しません、なぜですか?

それともコードを混ぜインデントです - あなたはタイマー機能(?なぜ、少なくとも機能は、すべてがあなたのinit関数内で宣言されている)を開始するために何もしないよう

は、私はそれが何もしないと仮定しますか?

編集1

だけ認識 - あなたが持っている循環依存(親は子と子コントロールの親コントロール) - そうする本当の決定的な理由がある場合はそれを避けるだけでそれをやろう。

(例98%が、私は仮定)以下の長所があるそれは異なる処理する方法があります。

  • 読みやすく簡単に
をデバッグする
  • ずっと簡単に維持するために

    編集2

    がありますは初期化されましたか?

    def update_timeText():

    があなたの問題の少なくとも一つを解決した直後にstate = False class LvlOneR(tk.Frame):上記とglobal stateを置きます。あなたの関数は状態変数を認識します。

  • +0

    初めてこのコードを使用して申し訳ありません。見やすくするためにどのようなコードを入力する必要がありますか?ストップウォッチは、自分の作成したフレームに追加したときにはうまく動作しません。私はあなたの編集2を試しましたが、私はグローバル変数をupdate_timeTextの後に置くとタイマ変数を認識しません。 – James

    +0

    ... Windows上で動作します...あなたはどっちですか? – R4PH43L

    +0

    Windows。私は問題を整理しました。とにかく助けてくれてありがとう – James

    関連する問題