1

My title may be kind of confusing, but I essentially want to draw a different set of shapes every four seconds then start over. For example, second one I want to draw triangles, second two I want to draw lines, second three I want to draws rectangles, and second four I want to draw ovals. The for second five I want this process to repeat. My Idea was to use the remainder which works up to second seven. Also is this possible to do without importing any additional modules. Any help would be great and if something was not clear please ask.

Thanks, Scott

Here is my current code to run if you want:

from tkinter import *
from random import *


#creates a class
class mainWindow():
    def __init__(self,theWindow,winName):

        #Sets values for variable
        self.running = 0

        #makes theWindow and instnace var
        self.theWindow = theWindow

        #creates the main frames
        self.mainFrame = Frame(theWindow,width=600,height=200,bg="light gray")
        self.secondframe = Frame(theWindow,width=600, height =287, bg = "green")

        self.mainFrame.grid()
        self.secondframe.grid()

        # gives the window a title
        theWindow.title(winName)

        # do not resize window based on widgets
        self.mainFrame.grid_propagate(False)

        # window location
        theWindow.geometry('+500+300')

        #Makes it so the window wont resize
        self.theWindow.resizable(0,0)

        #
        self.theWindow.wm_attributes("-topmost",1)


`enter code here`#Creates the three frames that will be used

        #Inerts a blank label to help with formating
        self.blank1 = Label(self.mainFrame, bg= "light gray", height =3, width=19)
        self.blank1.grid(row=0, column=0, rowspan =1)

        #Creates and places the start button
        self.startbutton = Label(self.mainFrame,text = "Start", bg= "green", height =1, width=10,relief=RAISED)
        self.startbutton.bind("<Button-1>", self.startTimer)
        self.startbutton.grid(row=0, column=2, rowspan =1, sticky = E)

        #Creates and places the stop button
        self.stopbutton = Label(self.mainFrame,text = "Stop", bg= "red", height =1, width=10,relief=RAISED)
        self.stopbutton.bind("<Button-1>", self.endTimer)
        self.stopbutton.grid(row=0, column=3, rowspan =1, sticky =W)

        #Creates and places the timer
        self.timerLabel = Label(self.mainFrame,text = "Time: 0", bg= "white", height =2, width=14,relief=SUNKEN)
        self.timerLabel.bind("<Button-1>",)
        self.timerLabel.grid(row=3, column=2, columnspan =2, pady =25)

        #draws the canvas
        self.drawCanvas = Canvas(self.secondframe,width=598,height=285, bg= "light green")
        self.drawCanvas.grid()

    #Function for strting the timer
    def startTimer(self,event):
        if self.running == 0:
            self.running = 1
            self.countElapse(0)

    #function for ening the timer
    def endTimer(self,event):
        if self.running == 1:
            self.running = 0
            self.timecount = 0
            self.drawCanvas.delete(ALL)
            self.timerLabel.configure(text = "Time: %d" %0)


    #function for showing the time
    def countElapse(self,localTime = NONE):
        #self.timerLabel.configure()
        if self.running:
            if localTime is not NONE:
                self.timecount = localTime
            self.timerLabel.configure(text ="Time: "+str(self.timecount+1))
            self.timecount = self.timecount+1
            self.drawshapes(self.timecount)
            self.mainFrame.after(1000,self.countElapse)

    #function for drawing the shpaes
    def drawshapes(self,timecount):

        self.drawCanvas.delete(ALL)
        color = ["red","white","purple","green","lime green", "cyan", "black","light blue","blue","pink","brown","gray"]
        numshapes = 100
        counter = 0
        print(self.timecount)

        var = self.timecount

        #draws ovals
        if var % 4 ==0:
            while counter != numshapes:
                counter += 1
                x = randint(10,600)
                y = randint(10,600)
                x1 = randint(10,600)
                y1 = randint(10,600)

                #draws the shape
                self.drawCanvas.create_oval(x,y,x1,y1,fill=choice(color))

        #draws rectangles
        elif var % 3 ==0:
            while counter != numshapes:
                counter += 1
                x = randint(10,600)
                y = randint(10,600)
                x1 = randint(10,600)
                y1 = randint(10,600)
                self.drawCanvas.create_rectangle(x,y,x1,y1,fill= choice(color))

        #draws Lines
        elif var % 2 ==0:
            while counter != numshapes:
                counter += 1
                x = randint(10,600)
                y = randint(10,600)
                x1 = randint(10,600)
                y1 = randint(10,600)
                self.drawCanvas.create_line(x,y,x1,y1,fill= choice(color))

        #draws triangles
        elif var % 1 ==0:
            while counter != numshapes:
                counter += 1
                x = randint(10,600)
                y = randint(10,600)
                x1 = randint(10,600)
                y1 = randint(10,600)
                x2 = randint(10,600)
                y2 = randint(10,600)
                self.drawCanvas.create_polygon(x,y,x1,y1,x2,y2,fill= choice(color))



def main():
    # create a tkinter object
    mainWin = Tk()
    # create a mainWindow object
    theMainWin = mainWindow(mainWin,"Scott Rodgers")
    # keep the window displaying
    mainWin.mainloop()


#calls main
main()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Srodgers
  • 11
  • 2
  • Possible duplicate of [What is the best way to repeatedly execute a function every x seconds in Python?](http://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in-python) – litepresence Apr 01 '17 at 18:19
  • do some research on the tkinter method `after`, which can be used to schedule something to run in the future. There are many examples on stackoverflow. – Bryan Oakley Apr 01 '17 at 23:13

1 Answers1

0

What you are looking for is how to use the .after method.

Here is how it works.

Lbl = Label()
Lbl.pack()
Lbl.after([Amount of milliseconds], lamba: [Function])

For more advanced notation,

after(delay_ms, callback=None, *args)

What this does is that it registers an alarm callback that is called after a given time

XzibitGG
  • 330
  • 5
  • 18