gpt4 book ai didi

python - 如何使我的功能不能与另一个功能同时工作?

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:38 25 4
gpt4 key购买 nike

我正在尝试创造一个运气游戏,你必须在 3 扇门后面挑选 2 个相同颜色的球,然后你尝试 3 次,每扇门都有 1 个球,我在你打开一扇门的地方被挡住了,然后我想在门显示球的那一刻和球消失的那一刻之间做一个延迟。我已经尝试使用 time.sleep 但它会在显示时运行 sleep 。这是我的代码:

import tkinter as tk
import tkinter as tk
from random import shuffle
import time
fenetre = tk.Tk()
fenetre['bg']='black'
fenetre.geometry("1152x768")
color = ["red", "green", "yellow"]
shuffle(color)

frameGauche = tk.Frame(width=200, height=600, bg='pink')
frameGauche.grid(row=0, column=0, padx=10, pady=10)

frameDroite = tk.Frame(width=700, height=700, bg='grey')
frameDroite.grid(row=0, column=1, padx=10, pady=10)

portegauche=tk.Frame(frameDroite, width=200,
height=600,bg='white',bd=5,relief='groove')
portegauche.grid(row=0, column=0, padx=5, pady=5)

portemilieu=tk.Frame(frameDroite, width=200,
height=600,bg='white',bd=5,relief='groove')
portemilieu.grid(row=0, column=1, padx=5, pady=5)

portedroite=tk.Frame(frameDroite, width=200,
height=600,bg='white',bd=5,relief='groove')
portedroite.grid(row=0, column=2, padx=5, pady=5)

def show1(canvas1, bouton2, bouton3):
canvas1.grid(row=0, column=1)
bouton2['state']='disabled'
bouton3['state']='disabled'
time.sleep(2)
bouton2['state']='normal'
bouton3['state']='normal'
canvas1.grid_remove()
def show2():
canvas2.grid(row=0, column=2)
bouton1['state']='disabled'
bouton3['state']='disabled'
time.sleep(2)
bouton1['state']='normal'
bouton3['state']='normal'
canvas2.grid_remove()
def show3():
canvas3.grid(row=0, column=3)
bouton2['state']='disabled'
bouton1['state']='disabled'
time.sleep(2)
bouton2['state']='normal'
bouton1['state']='normal'
canvas3.grid_remove()

canvas1=tk.Canvas(portegauche,width=200, height=600, bg='white')
c1 = canvas1.create_oval((60,280), (140,340), width=1, outline="black",
fill=color[0])
canvas1.grid_forget()

canvas2=tk.Canvas(portemilieu,width=200, height=600, bg='white')
c2 = canvas2.create_oval((60,280), (140,340), width=1, outline="black",
fill=color[1])
canvas2.grid_forget()

canvas3=tk.Canvas(portedroite,width=200, height=600, bg='white')
c3 = canvas3.create_oval((60,280), (140,340), width=1, outline="black",
fill=color[2])
canvas3.grid_forget()

def recommencer():
canvas1.grid_remove()
canvas2.grid_remove()
canvas3.grid_remove()
shuffle(color)
canvas1.create_oval((60,280), (140,340), width=1, outline="black", fill=color[0])
canvas2.create_oval((60,280), (140,340), width=1, outline="black", fill=color[1])
canvas3.create_oval((60,280), (140,340), width=1, outline="black", fill=color[2])
bouton1['state']='normal'
bouton2['state']='normal'
bouton3['state']='normal'

boutonR = tk.Button(frameGauche, text='Recommencer',command=recommencer)
boutonR.grid(row=0, column=0, padx=50, pady=50)

bouton1=tk.Button(frameDroite, text= 'Ouvrir',command=lambda: show1(canvas1,
bouton2, bouton3))
bouton1.grid(row=1, column=0)

bouton2=tk.Button(frameDroite, text= 'Ouvrir',command=show2)
bouton2.grid(row=1, column=1)

bouton3=tk.Button(frameDroite, text= 'Ouvrir',command=show3)
bouton3.grid(row=1, column=2)

fenetre.mainloop()

最佳答案

主要问题是您对 sleep() 的使用。在 tkinter 中休眠将导致整个实例卡住并且无法按预期工作。相反,您可以使用 after()

您还两次导入 tkinter。删除 tkinter 的导入。

也就是说,我们需要添加一个新函数并更改您的函数中的几行。

我添加了一个名为 normalize_button() 的函数,它接受 1 个按钮名称参数。这与 after() 方法一起使用,以在 2 秒后更新按钮。

在评论中回答你的问题:

normalize_button() 函数在 2 秒后被 after() 方法调用。该方法检查传递给它的字符串,并根据该字符串更新按钮。实际上,您可以使用 show 方法做同样的事情,如果您愿意,只需使用一种方法来更新所有按钮。它使事情变得更清晰,并遵循 DRY(不要重复自己)PEP8 风格。

看看下面的代码。

import tkinter as tk
from random import shuffle

fenetre = tk.Tk()
fenetre['bg']='black'
fenetre.geometry("1152x768")
color = ["red", "green", "yellow"]
shuffle(color)

frameGauche = tk.Frame(width=200, height=600, bg='pink')
frameGauche.grid(row=0, column=0, padx=10, pady=10)

frameDroite = tk.Frame(width=700, height=700, bg='grey')
frameDroite.grid(row=0, column=1, padx=10, pady=10)

portegauche=tk.Frame(frameDroite, width=200,
height=600,bg='white',bd=5,relief='groove')
portegauche.grid(row=0, column=0, padx=5, pady=5)

portemilieu=tk.Frame(frameDroite, width=200,
height=600,bg='white',bd=5,relief='groove')
portemilieu.grid(row=0, column=1, padx=5, pady=5)

portedroite=tk.Frame(frameDroite, width=200,
height=600,bg='white',bd=5,relief='groove')
portedroite.grid(row=0, column=2, padx=5, pady=5)

def normalize_button(btn_name):
print(btn_name)
if btn_name == "b1":
bouton1['state']='normal'
if btn_name == "b2":
bouton2['state']='normal'
if btn_name == "b3":
bouton3['state']='normal'

def show1():
canvas1.grid(row=0, column=1)
bouton2['state']='disabled'
bouton3['state']='disabled'
fenetre.after(2000, normalize_button, "b2")
fenetre.after(2000, normalize_button, "b3")
fenetre.after(2000, canvas1.grid_forget)

def show2():
canvas2.grid(row=0, column=2)
bouton1['state']='disabled'
bouton3['state']='disabled'
fenetre.after(2000, normalize_button, "b1")
fenetre.after(2000, normalize_button, "b3")
fenetre.after(2000, canvas2.grid_forget)

def show3():
canvas3.grid(row=0, column=3)
bouton2['state']='disabled'
bouton1['state']='disabled'
fenetre.after(2000, normalize_button, "b2")
fenetre.after(2000, normalize_button, "b1")
fenetre.after(2000, canvas3.grid_forget)

canvas1 = tk.Canvas(portegauche,width=200, height=600, bg='white')
c1 = canvas1.create_oval((60,280), (140,340), width=1, outline="black", fill=color[0])
canvas1.grid_forget()

canvas2 = tk.Canvas(portemilieu,width=200, height=600, bg='white')
c2 = canvas2.create_oval((60,280), (140,340), width=1, outline="black", fill=color[1])
canvas2.grid_forget()

canvas3 = tk.Canvas(portedroite,width=200, height=600, bg='white')
c3 = canvas3.create_oval((60,280), (140,340), width=1, outline="black", fill=color[2])
canvas3.grid_forget()

def recommencer():
canvas1.grid_remove()
canvas2.grid_remove()
canvas3.grid_remove()
shuffle(color)
canvas1.create_oval((60,280), (140,340), width=1, outline="black", fill=color[0])
canvas2.create_oval((60,280), (140,340), width=1, outline="black", fill=color[1])
canvas3.create_oval((60,280), (140,340), width=1, outline="black", fill=color[2])
bouton1['state']='normal'
bouton2['state']='normal'
bouton3['state']='normal'

boutonR = tk.Button(frameGauche, text='Recommencer',command=recommencer)
boutonR.grid(row=0, column=0, padx=50, pady=50)

bouton1=tk.Button(frameDroite, text= 'Ouvrir',command=show1)
bouton1.grid(row=1, column=0)

bouton2=tk.Button(frameDroite, text= 'Ouvrir',command=show2)
bouton2.grid(row=1, column=1)

bouton3=tk.Button(frameDroite, text= 'Ouvrir',command=show3)
bouton3.grid(row=1, column=2)

fenetre.mainloop()

关于python - 如何使我的功能不能与另一个功能同时工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50706071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com