0

I am trying to create a game of luck where you have to pick 2 balls of the same color behind 3 doors and you 3 attempt, each doors have 1 ball and i'm blocked at the point where you open one door and i want to make a delay between the moment the door show the balls and the moment the balls disappear. I already tried with a time.sleep but it runs the sleep at the time with the display. Here is my code:

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()
  • 1
    `sleep()` is not something you should use in tkinter. Use `after()` instead. Sleep will cause the entire instance to freeze. – Mike - SMT Jun 05 '18 at 18:05

2 Answers2

2

As mentioned already, sleep() will freeze execution and is never a good idea in GUI programming.

To get this working break up your showx functions into show and hide, here an example for show1. Here we are using .after to call a lambda function (which makes it easier to pass arguments), the first argument to .after is the time in milliseconds

def show1(canvas1, bouton2, bouton3):
    canvas1.grid(row=0, column=1)
    bouton2['state']='disabled'
    bouton3['state']='disabled'
    # OLD fenetre.after(2000, lambda: hide1(canvas1, bouton2, bouton3))
    # New thanks to Mike-SMT
    fenetre.after(2000, hide1, canvas1, bouton2, bouton3)

def hide1(canvas1, bouton2, bouton3):
    bouton2['state']='normal'
    bouton3['state']='normal'
    canvas1.grid_remove()

Here the Answer I got some details from Tkinter after

[EDIT] You can simplify this a lot by creating one show and one hide function and passing the canvas/button objects as arguments, there is more to refactor to make this simpler in the long run, but to get your started the usage of .after() should be what you need for now.

You may want to remove the double import of at the top of the file

import tkinter as tk
d parolin
  • 194
  • 6
  • your hide function is incomplete. – Mike - SMT Jun 05 '18 at 18:19
  • Hi @Mike-SMT, I am not sure in what sense, please help me improve my answer, given the original code it seems to do what is needed, I however give it to you that your answer is much more elaborate and the better one. – d parolin Jun 05 '18 at 18:27
  • Never mind. I see what you are doing now. However you do not need lambda here. Instead you can just do this. `fenetre.after(2000, hide1, canvas1, bouton2, bouton3)`. – Mike - SMT Jun 05 '18 at 18:38
  • I agree this can be reduced and actually this could be reduced to only 1 function that takes a few arguments and then performs the show and hide commands based of those arguments. – Mike - SMT Jun 05 '18 at 18:53
2

The main problem is your use of sleep(). Sleep in tkinter will cause the entire instance to freeze and wont work as you expect. Instead you can use after().

Also you are importing tkinter twice. Remove on of your imports for tkinter.

That said we will need to add a new function and change a couple lines in your functions.

I added a function called normalize_button() that takes 1 argument for the button name. This is used in tandem with the after() method to update the buttons after 2 seconds.

To answer your question in the comments:

The function normalize_button() is being called by the after() method after 2 seconds have passed. The method checks the string that is being passed to it and will update the button based off of that string. You can actually do the same thing with your show method and have just one method to update all the buttons if you like. It makes things a bit cleaner and follows the DRY(don't repeat yourself) PEP8 style.

Take a look at the below code.

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()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79