2016-09-01 2 views
0

pythonとtkinterを使用すると、プログラムの一部を実行する方法はありますか?ユーザーが特定のボタンをクリックして実行を続けるまで停止する方法はありますか?特定のアクションが起こるまでプログラムを停止する方法

意味:

機能 - 停止 - クリックボタン - 実行を継続します。このため

必要なコード:

def yellowClick(): 

    yellow.configure(activebackground="yellow3") 
    yellow.after(500, lambda: yellow.configure(activebackground="yellow")) 

yellow = Tkinter.Button(base, bd="0", highlightthickness="0", 
         width="7", height="5", activebackground="yellow", 
         bg="yellow3", command = yellowClick) 

yellow.place(x = 30, y = 50) 


def blueClick(): 

    blue.configure(activebackground="medium blue") 
    blue.after(500, lambda: blue.configure(activebackground="blue")) 

blue = Tkinter.Button(base, bd="0", highlightthickness="0", 
        width="7", height="5", activebackground="blue", 
        bg="medium blue", command = blueClick) 

blue.place(x = 125, y = 50) 


def redClick(): 

    red.configure(activebackground="red3") 
    red.after(500, lambda: red.configure(activebackground="red")) 

red = Tkinter.Button(base, bd="0", highlightthickness="0", 
        width="7", height="5", activebackground="red", 
        bg = "red3", command = redClick)  

red.place(x = 30, y = 145) 


def greenClick(): 

    green.configure(activebackground="dark green") 
    green.after(500, lambda: green.configure(activebackground="green4")) 

green = Tkinter.Button(base, bd="0", highlightthickness="0", 
         width="7", height="5", activebackground="green4", 
         bg="dark green", command = greenClick) 

green.place(x = 125, y = 145) 

def showSequence(): 

    r = random.randint(1, 4) 

    if r == 1: 

     yellow.configure(bg="yellow") 
     yellow.after(1000, lambda: yellow.configure(bg="yellow3")) 

    elif r == 2: 

     blue.configure(bg="blue") 
     blue.after(1000, lambda: blue.configure(bg="medium blue")) 

    elif r == 3: 

     red.configure(bg="red") 
     red.after(1000, lambda: red.configure(bg="red3")) 

    elif r == 4: 

     green.configure(bg="green4") 
     green.after(1000, lambda: green.configure(bg="dark green")) 

これは、サイモンゲームのためですが、私はプレイヤーがボタンをクリックするまで、それは停止させる、その後、一度この関数を実行し、このfunction.Thisに戻る必要があります最初のターンのために。私はshowsequence関数を、ボタンがクリックされるまで停止する方法で接続する必要がありますが、私はその方法を知らないのです。

時間をかけてプログラムを停止することはできません。具体的な処理が行われるのを待ちます。

+0

可能であれば、簡単なコードを追加してください。誰かがより良い方法を投稿するかもしれませんが、あなたはいつも100msのスリープ機能を持つwhileループを使うことができます。例: 'while True:何か他のものを調べるtime.sleep(0.1)' – 16num

答えて

-1

このコードサンプルをご覧ください。そのまま動作し、コピー/ペーストして実行します。ループはありません。ボタンをクリックするのを待ちます。私は基本的にあなたのレベルかもしれない "答え"リストに色のランダムなパターンを読み込むことによってプログラムを初期化できると思います。
make TK inter wait for user input

関連する問題