文字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によるプログラミングに精通していません。
tkinterのメインループ**は、インターフェイスとすべてを更新する** whileループです。あなた自身をそこに置くと、GUIは何もすることができません。 '.after()'は、自分自身をスケジューリングする関数とともに使用する必要があります。 – Delioth
あなたは 'after(0、...)'を使うべきではありません。イベントループは、「アフター」キューが空にならないため、他のイベントを処理する機会を得ることはありません。あなたは常にゼロ以外の時間を与えるべきです。これは唯一の問題ではないかもしれませんが、おそらく問題の一部です。 –