2017-04-01 2 views
1

Mein Titel mag etwas verwirrend sein, aber ich möchte im Grunde alle vier Sekunden einen anderen Satz von Formen zeichnen und dann neu anfangen. Zum Beispiel, zweitens möchte ich Dreiecke zeichnen, zweite zwei möchte ich Linien zeichnen, zweite drei möchte ich Rechtecke zeichnen, und zweitens vier möchte ich Ovale zeichnen. Die zweite Fünf möchte ich wiederholen. Meine Idee war es, den Rest zu verwenden, der bis zur zweiten Sieben reicht. Dies ist auch möglich, ohne zusätzliche Module zu importieren. Jede Hilfe wäre großartig und wenn etwas nicht klar war, fragen Sie bitte.Wie man verschiedene Aktionen jede Sekunde und dann alle vier Sekunden wiederholen kann

Danke, Scott

Hier ist meine aktuellen Code auszuführen, wenn Sie wollen:

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() 
+0

Mögliche Duplikat [Was ist der beste Weg, immer wieder genannt wird, in Python alle x Sekunden eine Funktion ausführen?] (http://stackoverflow.com/questions/474528/what-ist-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in- -python) – litepresence

+0

forschen Sie an der tkinter-Methode 'after', die verwendet werden kann, um etwas zu planen, das in der Fu läuft ture. Es gibt viele Beispiele für Stackoverflow. –

Antwort

0

Was Sie suchen ist, wie die .after Methode zu verwenden.

Hier ist, wie es funktioniert.

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

Für erweiterte Notation

after(delay_ms, callback=None, *args) 

Was dies bedeutet ist, dass es einen Alarmrückruf registriert, die nach einer bestimmten Zeit

Verwandte Themen