1

I understand that this question is repetitive, but for my code, none of the other answers(using after, time.sleep) have helped at all. Here is my code:

from tkinter import *
import time,random
root = Tk()

class Simon:
    def __init__(self):
        print('SIMON')
        self.red = PhotoImage(file="Red.gif")
        self.blue = PhotoImage(file="Blue.gif")
        self.green = PhotoImage(file = "Green.gif")
        self.yellow = PhotoImage(file= "Yellow.gif")
        self.white = PhotoImage(file= "Blankimage.gif")
        self.randlist=[]
        self.storerand=0

    def createpattern(self):
    ##    for n in range(4):
    ##        self.storerand=random.randint(1,4)
    ##        self.randlist.append(self.storerand)
    ##        self.storerand=0
    ##    print(self.randlist)
        self.randlist=[1]

    def pause(self):
        for n in range(0,len(self.randlist)):
            if self.randlist[n]==1:
                self.button1=Button(root, image = self.red)
                self.button2=Button(root, image=self.white)
                self.button3=Button(root,image = self.white)
                self.button4=Button(root, image = self.white)
                print('1')
            elif self.randlist[n]==2:
                self.button1=Button(root,image = self.white)
                self.button2=Button(root, image=self.blue)
                self.button3=Button(root,image = self.white)
                self.button4=Button(root, image = self.white)
                print('2')
            elif self.randlist[n]==3:
                self.button1=Button(root,image = self.white)
                self.button2=Button(root, image=self.white)
                self.button3=Button(root,image = self.green)
                self.button4=Button(root, image = self.white)
                print('3')
            elif self.randlist[n]==4:
                self.button1=Button(root,image = self.white)
                self.button2=Button(root, image=self.white)
                self.button3=Button(root,image = self.white)
                self.button4=Button(root, image = self.yellow)
                print('4')

    def grid(self):
        self.button1.grid(row=0,column=0)
        self.button2.grid(row=0,column=1)
        self.button3.grid(row=1, column=0)
        self.button4.grid(row=1,column=1)

simon=Simon()
simon.createpattern()
simon.pause()
root.mainloop()

I need to make a blink function (which is currently my pause function), but if I use sleep it stops my code and I don't really know how to implement the after method in this case. Any suggestions?

martineau
  • 119,623
  • 25
  • 170
  • 301
andrewJ
  • 111
  • 2
  • 11
  • 4
    Even if you claim it doesn’t help, after is what you at after (pun intended). – deets Apr 19 '18 at 23:04
  • but how would I implement it? – andrewJ Apr 19 '18 at 23:05
  • Do I understand it correctly, that you want it to wait at the end of each iteration of that `for` loop? – Dan Mašek Apr 19 '18 at 23:05
  • 2
    You want to create a timer, is here the answer you want https://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter – Aurélien Tronc Apr 19 '18 at 23:06
  • yes, that is exactly what I am trying to do – andrewJ Apr 19 '18 at 23:06
  • Then the loop body should be the function you schedule using `after`. You will need to pass it the value of `n` to use -- probably use `functools.partial` for that. At the end of that function you increment `n`, and schedule another "iteration" if necessary. – Dan Mašek Apr 19 '18 at 23:08
  • It unclear to me what you're trying to accomplish. In the code, `self.randlist` is empty, so references to it like `self.randlist[n]` will throw an exception. Also have no idea what you mean by a `blink` function. Please [edit] your question and try to explain what's supposed to happen. – martineau Apr 20 '18 at 00:58

0 Answers0