Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在tkinter(python 3)中,如何使程序在继续之前等待事件(例如,按钮单击)?_Python_Tkinter - Fatal编程技术网

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

在tkinter(python 3)中,如何使程序在继续之前等待事件(例如,按钮单击)?,python,tkinter,Python,Tkinter,我正在做一个简单的石头剪刀程序(我有史以来的第一个程序,以前没有编程经验),这就是我想到的 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, col

我正在做一个简单的石头剪刀程序(我有史以来的第一个程序,以前没有编程经验),这就是我想到的

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()
从tkinter导入*
随机输入
computerChoice=random.randint(1,3)
playerChoice=0
root=Tk()
根几何体('315x400')
标签=标签(text=“石头、布或剪刀?”)
标签网格(行=0,列=0)
ttf=帧(根)
ttf.grid(行=1,列=0)
tbf=帧(根)
tbf.网格(行=2,列=0)
mtf=帧(根)
mtf.网格(行=3,列=0)
mbf=帧(根)
mbf.网格(行=4,列=0)
btf=帧(根)
btf.网格(行=5,列=0)
bbf=帧(根)
bbf.网格(行=6,列=0)
photo1=PhotoImage(file=“rock.png”)
photo2=PhotoImage(file=“paper.png”)
photo3=PhotoImage(file=“scissors.png”)
playerLabel1=标签(待定)
playerLabel2=标签(待定)
playerLabel3=标签(待定)
def按钮1命令(事件):
playerLabel1=标签(text=“您选择了摇滚乐\n”)
playerLabel1.grid(行=2,列=0)
返回playerChoice==1
如果computerChoice==1:
computerLabel1=标签(mtf,text=“你的对手选择:”)
computerLabel1.grid(行=3,列=0)
computerLabel11=标签(mbf,图像=照片1,宽度=“100”,高度=“100”)
computerLabel11.grid(行=4,列=0)
labelResult3=Label(text=“打成平局!”)
labelResult3.grid(行=5,列=0)
elif computerChoice==2:
computerLabel3=标签(mtf,text=“你的对手选择:”)
computerLabel3.grid(行=3,列=0)
computerLabel33=标签(mbf,图像=照片2,宽度=“100”,高度=“100”)
computerLabel33.grid(行=4,列=0)
labelResult2=Label(text=“你输了!”)
labelResult2.grid(行=5,列=0)
elif computerChoice==3:
computerLabel3=标签(mtf,text=“你的对手选择:”)
computerLabel3.grid(行=3,列=0)
computerLabel33=标签(mbf,图像=照片3,宽度=“100”,高度=“100”)
computerLabel33.grid(行=4,列=0)
labelResult1=Label(text=“您赢了!”)
labelResult1.grid(行=5,列=0)
def按钮2命令(事件):
playerLabel2=标签(text=“您选择了纸张\n”)
playerLabel2.grid(行=2,列=0)
返回播放器选择==2
如果computerChoice==1:
computerLabel3=标签(mtf,text=“你的对手选择:”)
computerLabel3.grid(行=3,列=0)
computerLabel33=标签(mbf,图像=照片1,宽度=“100”,高度=“100”)
computerLabel33.grid(行=4,列=0)
labelResult1=Label(text=“您赢了!”)
labelResult1.grid(行=5,列=0)
elif computerChoice==2:
computerLabel2=标签(mtf,text=“你的对手选择:”)
computerLabel2.grid(行=3,列=0)
computerLabel22=标签(mbf,图像=照片2,宽度=“100”,高度=“100”)
computerLabel22.grid(行=4,列=0)
labelResult3=Label(text=“打成平局!”)
elif computerChoice==3:
computerLabel3=标签(mtf,text=“你的对手选择:”)
computerLabel3.grid(行=3,列=0)
computerLabel33=标签(mbf,图像=照片3,宽度=“100”,高度=“100”)
computerLabel33.grid(行=4,列=0)
labelResult2=Label(text=“你输了!”)
labelResult2.grid(行=5,列=0)
def按钮3命令(事件):
playerLabel3=标签(text=“您选择了剪刀\n”)
playerLabel3.grid(行=2,列=0)
返回播放器选择==3
如果computerChoice==1:
computerLabel3=标签(mtf,text=“你的对手选择:”)
computerLabel3.grid(行=3,列=0)
computerLabel33=标签(mbf,图像=照片1,宽度=“100”,高度=“100”)
computerLabel33.grid(行=4,列=0)
labelResult2=Label(text=“你输了!”)
labelResult2.grid(行=5,列=0)
elif computerChoice==2:
computerLabel3=标签(mtf,text=“你的对手选择:”)
computerLabel3.grid(行=3,列=0)
computerLabel33=标签(mbf,图像=照片2,宽度=“100”,高度=“100”)
computerLabel33.grid(行=4,列=0)
labelResult1=Label(text=“您赢了!”)
labelResult1.grid(行=5,列=0)
elif computerChoice==3:
computerLabel3=标签(mtf,text=“你的对手选择:”)
computerLabel3.grid(行=3,列=0)
computerLabel33=标签(mbf,图像=照片3,宽度=“100”,高度=“100”)
computerLabel33.grid(行=4,列=0)
labelResult3=Label(text=“打成平局!”)
按钮1=按钮(ttf)
按钮1.config(图像=photo1,width=“100”,height=“100”)
button1.bind(“,button1命令)
button1.bind(“,button1命令)
button1.bind(“,button1命令)
按钮1.网格(行=1,列=0)
按钮2=按钮(ttf)
按钮2.config(图像=photo2,width=“100”,height=“100”)
button2.bind(“,button2命令)
button2.bind(“,button2命令)
button2.bind(“,button2命令)
按钮2.网格(行=1,列=1)
button3=按钮(ttf)
button3.config(image=photo3,width=“100”,height=“100”)
按钮3.绑定(“,按钮3命令)
按钮3.绑定(“,按钮3命令)
按钮3.绑定(“,按钮3命令)
按钮3.网格(行=1,列=2)
root.mainloop()
问题是,该程序只是为computerChoice选择了一个随机整数,并按预期显示照片,并写入第一个与该computerChoice整数关联的消息。它没有考虑playerChoice,该选项应与按钮单击后发生的事件一起设置。 现在我希望它只显示前2行,“选择”行和3个照片选项行,单击按钮后,它应该显示其他行

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


ps:如前所述,我的第一点编程,如果这是一个愚蠢或复杂的问题,非常抱歉:)

您只需创建一个标志变量:

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
关键字。 但它仍然有效。 希望这会有帮助。
当然这很愚蠢。

您不应该从按钮的事件返回值,因为存在