使用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"))
这是一个西蒙游戏,我需要运行一次这个功能,然后让它停止,直到玩家点击一个按钮,然后返回到这个功能。这是第一个回合。我需要以一种停止的方式连接showsequence函数,直到单击一个按钮但我不知道如何。
按时间限制程序将不起作用,我的意思是等待特定的操作发生。
答案 0 :(得分:-1)
看一下这段代码示例。它按原样工作,复制/过去它将运行。没有循环。它等待按钮单击。我认为你可以通过将随机的颜色模式加载到"答案中来初始化程序。列表可能基本上是你的水平 make TK inter wait for user input