私はゲームを持っています。ボタンが作成されると、このコードを「次のレベル」を押すまで表示するだけですwhileループはゲームを制御します。Tkinterをボタンが押されるまで待つようにする
......
if game.playerDistance >= game.lowerBound() and game.playerDistance <= game.upperBound():
game.level += 1
showLevelResults(game)
#NextLevelButton
btnNextLevel = Button(root,
#Random Config
command = nextLevel,
)
btnNextLevel.place(x=1003, y=492, anchor=NW, width=247, height=78)
updateMainScreen()
while nextLev == False:
#What Do I put in here to force a wait
else:
......
nextLev = False
def nextLevel():
nextLev = True
...
は現在、このwhileループでそれを維持し、ボタンがあるとき私はtime.sleep(1)を使用して待機していましたが、btn pressを待っていましたが、これはコンソールをスパムし、ボタンが押されても画面を変更しません。
def showGameSurvival():
game = gamemode_normal()
while game.health != 0:
game.next = False
clearScreen()
changeBackground("Survival")
#Placing Labels on the screen for game.....
#... Health
root.update()
lblCountDownLeft = Label(root, bg="White", fg="Green", font=XXLARGE_BUTTON_FONT)
lblCountDownLeft.place(x=169, y=350, anchor=CENTER)
lblCountDownRight = Label(root, bg="White", fg="Green", font=XXLARGE_BUTTON_FONT)
lblCountDownRight.place(x=1111, y=350, anchor=CENTER)
#CountDown
count = 7
while count > 0:
lblCountDownLeft['text'] = count
lblCountDownRight['text'] = count
root.update()
count -= 1
time.sleep(1)
lblCountDownLeft.destroy()
lblCountDownRight.destroy()
root.update()
#Num on left x=169, right, x=1111 y=360
game.measureDistance()
if game.playerDistance >= game.lowerBound() and game.playerDistance <= game.upperBound():
game.level += 1
clearScreen()
changeBackground("Survival")
graphicalDisplay(game)
#NextLevelButton
btnNextLevel = Button(root,
bg= lbBlue,
fg="white",
text="Level" + str(game.level),
font=SMALL_BUTTON_FONT,
activebackground="white",
activeforeground= lbBlue,
command= lambda: nextLevel(game),
bd=0)
btnNextLevel.place(x=1003, y=492, anchor=NW, width=247, height=78)
root.update()
while game.next == False:
print(game.next)
else:
game.health -= 1
if game.allowance > 4:
game.allowance = int(game.allowance*0.9)
#when game is over delete the shit
if game.health == 0:
del game
次のボタンは今、この関数を呼び出します:def nextLevel(game): game.next = True
tkinterで 'sleep()'を使わないでください。代わりに 'after()'を使用してください。スリープを使用すると、tkinterインスタンス全体が一時停止し、おそらくあなたが思っていることをしていないでしょう。 –
[mcve]を作成してください。一般的なルールとして、tkinterにはすでに無限ループが存在するため、あなた自身のゲームループを回避する必要があります。ゲームループをどのように実装したかを確認する必要があります。 –
ねえ、私の質問を更新し、私のコードの多くの重要でないセクションを削除しました。私はちょうどそのボタンが作成された後、それが何であり、何も変化しないために私の画面が必要です、そして、そのボタンがクリックされるまで、それは非常にお世話になりました! –