2017-06-27 314 views
1

私はゲームを持っています。ボタンが作成されると、このコードを「次のレベル」を押すまで表示するだけです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

+0

tkinterで 'sleep()'を使わないでください。代わりに 'after()'を使用してください。スリープを使用すると、tkinterインスタンス全体が一時停止し、おそらくあなたが思っていることをしていないでしょう。 –

+2

[mcve]を作成してください。一般的なルールとして、tkinterにはすでに無限ループが存在するため、あなた自身のゲームループを回避する必要があります。ゲームループをどのように実装したかを確認する必要があります。 –

+0

ねえ、私の質問を更新し、私のコードの多くの重要でないセクションを削除しました。私はちょうどそのボタンが作成された後、それが何であり、何も変化しないために私の画面が必要です、そして、そのボタンがクリックされるまで、それは非常にお世話になりました! –

答えて

4

いくつかのイベントは、「待つ」機能の一つ、などwait_variablewait_window、またはwait_visibility呼び出すことであるのを待つのTkinterを取得する最も簡単な方法。

ボタンクリックを待つ場合は、wait_variableを使用して、ボタンに変数を設定させることができます。ボタンをクリックすると変数が設定され、変数が設定されるとwait_variableへの呼び出しが返されます。例えば

import tkinter as tk 
root = tk.Tk() 
... 
var = tk.IntVar() 
button = tk.Button(root, text="Click Me", command=lambda: var.set(1)) 
button.place(relx=.5, rely=.5, anchor="c") 

print("waiting...") 
button.wait_variable(var) 
print("done waiting.") 

注:IntVarを使用する必要はありません - 特別Tkinterの変数のいずれかが行います。また、何を設定するかは関係ありません。メソッドは変更するまで待機します。

+0

これはちょうど私が少しエラーを得るのを見ているように聞こえる:私はあなたの例のtkをroot、tkに変え、私のプログラムの一番上にroot = Tk()があるこれらのエラーのいずれかを取得し、どちらを使用する必要がありますか? var = Tk.IntVar(): AttributeError:型オブジェクト 'Tk'には属性がありません 'IntVar' ||| var = root.IntVar(): AttributeError: '_tkinter.tkapp'オブジェクトに 'IntVar'属性がありません ||| var = tk.IntVar(): NameError:名前 'tk'が定義されていません –

+0

var = IntVar()を実行して修正しました ブライアン非常にありがとう、私はこれについて知りませんでした。ゲーム!! ありがとうございます! –

関連する問題