0

Using python and tkinter, is there a way I can run a part of the program, then stop it until the user clicks a specific button and then continue running?

I mean:

function - stop - click button - continue running.

Necessary code for this:

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

This is for a simon game, I need to run this function once, then make it stop until the player clicks a button and then return to this function.This is for the first turn. I need to connect the showsequence function in a way that it stops until a button is clicked but I don't know how.

Stoping the program by time will not work in this, I mean wait for a specific action to happen.

Andrea
  • 43
  • 5
  • if possible please add a simple piece of code. Someone may post a better method but you could always use a while loop with a 100ms sleep function. eg: `while True: check for something else time.sleep(0.1)` – J'e Sep 01 '16 at 18:35

1 Answers1

-1

have a look at this code sample. it works as is, copy/past it and it will run. There is no loop. It waits for the button click. I think you could initialize the program by loading a random pattern of colors into the "answers" list which could essentially be your levels.
make TK inter wait for user input

Community
  • 1
  • 1
J'e
  • 3,014
  • 4
  • 31
  • 55