简体   繁体   中英

In tkinter (python 3) how to make the program wait for an event (e.g. button click) before continuing?

I'm making a simple Rock paper scissors program (my first program ever, no prior programming experience) and this is what I came up with.

from tkinter import *

import random

computerChoice = random.randint(1, 3)
playerChoice = 0
root = Tk()
root.geometry('315x400')
theLabel = Label(text="Rock, paper or scissors?")
theLabel.grid(row=0, column=0)

ttf = Frame(root)
ttf.grid(row=1, column=0)
tbf = Frame(root)
tbf.grid(row=2, column=0)
mtf = Frame(root)
mtf.grid(row=3, column=0)
mbf = Frame(root)
mbf.grid(row=4, column=0)
btf = Frame(root)
btf.grid(row=5, column=0)
bbf = Frame(root)
bbf.grid(row=6, column=0)

photo1 = PhotoImage(file="rock.png")
photo2 = PhotoImage(file="paper.png")
photo3 = PhotoImage(file="scissors.png")
playerLabel1 = Label(tbf)
playerLabel2 = Label(tbf)
playerLabel3 = Label(tbf)

def button1Command(event):
    playerLabel1 = Label(text="You chose rock\n")
    playerLabel1.grid(row=2, column=0)
    return playerChoice == 1
if computerChoice == 1:
    computerLabel1 = Label(mtf, text="Your opponent chose:")
    computerLabel1.grid(row=3, column=0)
    computerLabel11 = Label(mbf, image=photo1, width="100", height="100")
    computerLabel11.grid(row=4, column=0)
    labelResult3 = Label(text="It's a tie!")
    labelResult3.grid(row=5, column=0)
elif computerChoice == 2:
    computerLabel3 = Label(mtf, text="Your opponent chose:")
    computerLabel3.grid(row=3, column=0)
    computerLabel33 = Label(mbf, image=photo2, width="100", height="100")
    computerLabel33.grid(row=4, column=0)
    labelResult2 = Label(text="You lose!")
    labelResult2.grid(row=5, column=0)
elif computerChoice == 3:
    computerLabel3 = Label(mtf, text="Your opponent chose:")
    computerLabel3.grid(row=3, column=0)
    computerLabel33 = Label(mbf, image=photo3, width="100", height="100")
    computerLabel33.grid(row=4, column=0)
    labelResult1 = Label(text="You win!")
    labelResult1.grid(row=5, column=0)


def button2Command(event):
    playerLabel2 = Label(text="You chose paper\n")
    playerLabel2.grid(row=2, column=0)
    return playerChoice == 2
if computerChoice == 1:
    computerLabel3 = Label(mtf, text="Your opponent chose:")
    computerLabel3.grid(row=3, column=0)
    computerLabel33 = Label(mbf, image=photo1, width="100", height="100")
    computerLabel33.grid(row=4, column=0)
    labelResult1 = Label(text="You win!")
    labelResult1.grid(row=5, column=0)
elif computerChoice == 2:
    computerLabel2 = Label(mtf, text="Your opponent chose:")
    computerLabel2.grid(row=3, column=0)
    computerLabel22 = Label(mbf, image=photo2, width="100", height="100")
    computerLabel22.grid(row=4, column=0)
    labelResult3 = Label(text="It's a tie!")
elif computerChoice == 3:
    computerLabel3 = Label(mtf, text="Your opponent chose:")
    computerLabel3.grid(row=3, column=0)
    computerLabel33 = Label(mbf, image=photo3, width="100", height="100")
    computerLabel33.grid(row=4, column=0)
    labelResult2 = Label(text="You lose!")
    labelResult2.grid(row=5, column=0)


def button3Command(event):
    playerLabel3 = Label(text="You chose scissors\n")
    playerLabel3.grid(row=2, column=0)
    return playerChoice == 3
if computerChoice == 1:
    computerLabel3 = Label(mtf, text="Your opponent chose:")
    computerLabel3.grid(row=3, column=0)
    computerLabel33 = Label(mbf, image=photo1, width="100", height="100")
    computerLabel33.grid(row=4, column=0)
    labelResult2 = Label(text="You lose!")
    labelResult2.grid(row=5, column=0)
elif computerChoice == 2:
    computerLabel3 = Label(mtf, text="Your opponent chose:")
    computerLabel3.grid(row=3, column=0)
    computerLabel33 = Label(mbf, image=photo2, width="100", height="100")
    computerLabel33.grid(row=4, column=0)
    labelResult1 = Label(text="You win!")
    labelResult1.grid(row=5, column=0)
elif computerChoice == 3:
    computerLabel3 = Label(mtf, text="Your opponent chose:")
    computerLabel3.grid(row=3, column=0)
    computerLabel33 = Label(mbf, image=photo3, width="100", height="100")
    computerLabel33.grid(row=4, column=0)
    labelResult3 = Label(text="It's a tie!")

button1 = Button(ttf)
button1.config(image=photo1, width="100", height="100")
button1.bind("<Button-1>", button1Command)
button1.bind("<Button-2>", button1Command)
button1.bind("<Button-3>", button1Command)
button1.grid(row=1, column=0)

button2 = Button(ttf)
button2.config(image=photo2, width="100", height="100")
button2.bind("<Button-1>", button2Command)
button2.bind("<Button-2>", button2Command)
button2.bind("<Button-3>", button2Command)
button2.grid(row=1, column=1)

button3 = Button(ttf)
button3.config(image=photo3, width="100", height="100")
button3.bind("<Button-1>", button3Command)
button3.bind("<Button-2>", button3Command)
button3.bind("<Button-3>", button3Command)
button3.grid(row=1, column=2)

root.mainloop()

problem is, the program just selects a random int for computerChoice and displays the photo as intended, and writes the message that is first associated with that computerChoice int. It doesn't take into account the playerChoice which should get set with the event that happens after the button click. Now I want it to just display the first 2 rows, the "choose" row and the 3 photo options row, and after I click a button it should display the other rows.

Now I don't understand if the problem is in my "return playerChoice == 1/2/3" part of the code, or if my if and elif parts are all useless. Why doesn't it take into account the playerChoice that happens with the button click initiated event?

ps: as mentioned, my first bit of programming ever, so sorry if it's a stupid or complicated question :)

You can simply create a flag variable:

from tkinter import *

evtHappened=False
def do():
    print('click!')
    global evtHappened
    evtHappened=True

tk = Tk()
Button(tk,command=do,text='click me!').pack()

while True:
    if evtHappened:
        evtHappened=False
        print('detected')
    tk.update()

Obviously, not the greatest solution, but it should work! Actually, it's the stupidest solution. Never use the global keyword. But it still works. Hope that's helpful. Sure that's stupid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM