如何让不同的动作每秒钟发生一次,然后每四秒钟重复一次

2024-10-04 07:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我的标题可能有点混乱,但我基本上想每四秒钟画一组不同的形状,然后重新开始。例如,第二个我想画三角形,第二个我想画直线,第二个我想画矩形,第二个我想画椭圆。第二个五分钟,我想重复这个过程。我的想法是使用余数,直到第二个7。也可以不导入任何附加模块。任何帮助将是伟大的,如果有什么不清楚,请问。你知道吗

谢谢你, 斯科特

如果您需要,下面是我当前要运行的代码:

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

Tags: theselfdefcounterwidthgridbgx1
1条回答
网友
1楼 · 发布于 2024-10-04 07:27:26

你要找的是如何使用.after方法。你知道吗

下面是它的工作原理。你知道吗

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

对于更高级的表示法

after(delay_ms, callback=None, *args)

它的作用是注册一个在给定时间后调用的报警回调

相关问题 更多 >