2016-12-15 11 views
0

文字A〜Gからランダムな単語を生成し、その単語をMIDIコントローラにスペルするように求めるミニゲームを作成しようとしています。以前は、whileループを使用してコマンドラインで正常に動作していたコードを持っていましたが、GUIを作成すると、whileループはうまく動作しません。after()が私のTkinter GUIを更新していない

私は研究を行いましたが、after()メソッドを使用してその関数をリフレッシュしてGUIウィンドウを更新することをお勧めしました。しかし、それはそうしていません。私はウィンドウが毎回押されたノートを印刷して、それをリストに印刷したいと思う。私は一日中、それを理解しようと努力しましたが、役に立たなかったのです。

これまでの私のコード:

def spelling_func(self): 
    """ Lesson 1: Spelling Game 
     MINI GAME that generates a random word & asks user to spell it out using piano keyboard.""" 

    piano = PianoInput() # Class that reads in MIDI input 
    spelling = SpellingGame() 
    text = Tkinter.Text(self.window.after(1), bg="black", fg="red", font="Helvetica") 

    # Generates random word from dict. 
    spelling.random_word_gen() 

    # Asks user to spell random word 
    text.insert('insert', "Please spell the word: " + spelling.word) 
    text.pack() 

    #while spelling.isTrue: 
    piano.detect_key() 
    if piano.piano_key == [] or piano.piano_key[0][0][0] == 128: 
     pass 
    else: 
     #Prints the note pressed. 
     text.insert('insert', piano.display_note(piano.piano_key[0][0][1])) 
     text.pack() 

     print piano.display_note(piano.piano_key[0][0][1]) 

     spelling.user_input = spelling.input_to_list(piano.piano_key[0][0][1]) 

     user_input_notes = [] 

     # Cycles through user input list & converts returned number to what key is pressed. Then adds to list. 
     for item in spelling.user_input: 
      temp_key = piano.display_note(item) 

      parsed_note = piano.parse_key(temp_key) 

      user_input_notes.append(parsed_note) 
      print "user input notes" 
      print user_input_notes 
      print "Generated word" 
      print spelling.generated_word 

      spelling.win_or_lose(user_input_notes) 

      # continue 
     self.after(0, self.spelling_func) 

元whileループは、単にアイデアを与えることだったところ私がコメントアウトしました。必要に応じて、このメソッドは別のクラスの別のメソッドを呼び出します。その定義は次のとおりです。

def win_or_lose(self, user_input_notes): 
    i = 0 # Counter for list positions. 

    # If notes are correct. 
    if user_input_notes == self.generated_word: 
     print 'CONGRATS!' 
     winsound.PlaySound('sound/sfx/OOT_Song_Correct.wav', winsound.SND_FILENAME) 

     # Clears out all previous input & generated words 
     self.generated_word = [] 
     self.user_input = [] 
     user_input_notes = [] 

     self.random_word_gen() 

     # Asks user to spell random word 
     print "Please spell the word: " + self.word 

    # If user gets note wrong. 
    elif user_input_notes[i] != self.generated_word[i]: 
     print 'WRONG!' 
     winsound.PlaySound('sound/sfx/OOT_Song_Error.wav', winsound.SND_FILENAME) 

     # Clears out all previous input 
     self.user_input = [] 
     user_input_notes = [] 
     i = 0 

     print "Try again!" 
     print "\n" 
     print "Please spell the word: " + self.word 

    else: 
     i += 1 

ありがとうございます。私はそれがうんざりであることをお詫びします、私はあまりGUIによるプログラミングに精通していません。

+0

tkinterのメインループ**は、インターフェイスとすべてを更新する** whileループです。あなた自身をそこに置くと、GUIは何もすることができません。 '.after()'は、自分自身をスケジューリングする関数とともに使用する必要があります。 – Delioth

+1

あなたは 'after(0、...)'を使うべきではありません。イベントループは、「アフター」キューが空にならないため、他のイベントを処理する機会を得ることはありません。あなたは常にゼロ以外の時間を与えるべきです。これは唯一の問題ではないかもしれませんが、おそらく問題の一部です。 –

答えて

0

私は解決策を考え出しました。私がafter(0、...)を使っていたことが判明したので、Bryanが言ったように、after()をスキップしました。私はそれを(1、...)の後に変更しました。最初の部分はループする必要がないので、#while isTrueがコメントアウトされている部分だけ、その関数を2つの異なる関数に分割しました。

次に、2番目の関数の最後にafter()を配置し、もう少し微調整して、うまくいきました。ありがとうございました:)

関連する問題