在tkinter(python 3)中如何让程序在继续之前等待事件(例如按钮点击)?

时间:2016-02-22 14:44:08

标签: python tkinter

我正在制作一个简单的Rock paper剪刀程序(我的第一个程序,没有先前的编程经验),这就是我想出来的。

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()

问题是,程序只为computerChoice选择一个随机int并按预期显示照片,并写入首先与该computerChoice int关联的消息。它没有考虑应该通过按钮点击后发生的事件设置的playerChoice。 现在我希望它只显示前两行,“选择”行和3个照片选项行,然后在我单击一个按钮后,它应显示其他行。

现在我不明白问题是否在我的“return playerChoice == 1/2/3”部分代码中,或者我的if和elif部分都是无用的。为什么不考虑按钮点击启动事件发生的playerChoice?

ps:如上所述,我的第一次编程,如果这是一个愚蠢或复杂的问题,那就很抱歉:)

1 个答案:

答案 0 :(得分:0)

您可以简单地创建一个标志变量:

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()

显然,这不是最大的解决方案,但它应该可以工作! 实际上,这是最愚蠢的解决方案。 切勿使用global关键字。 但它仍然有效。 希望对您有所帮助。 当然这很愚蠢。