python - tkinter: Analog clock refreshing - how does "after" function work? -


recently, i'm beginner in python, , i've programmed analog clock on tkinter, can display time every second. clock uses while 1 infinite loop refresh time, totally blocks rest of program. because want use in program, parallel function, had reprogram refreshing method.

i've found after() function can (i think) me, runtimeerror: maximum recursion depth exceeded in comparison , don't understand why. drawing of clock heavy python? i'll put code here , hope understand problem , can me.

(ps 1: don't take care code before "principle function" , don't think problem there, i'll still post because i'm not sure @ part debugged.

ps 2: i'm sorry english may problem, tell me if don't understand.)

#list of imported modules tkinter import * math import cos, sin time import gmtime, sleep  #defining of secondary functions  def drawcircle(alpha,beta,rayon,couleur,can): #draw circle base on center coord radius , color     x1,y1,x2,y2=alpha-rayon, beta-rayon, alpha+rayon, beta+rayon     can.create_oval(x1,y1,x2,y2,fill=couleur)  def drawpetaig(coorda, coordz, taille, omega, can): #function drawn second hand of clock     pi = 3.141592     omega = ((omega/60)+1)*30     can.create_line(coorda + (taille/3) * cos(pi*(omega/180)), coordz + (taille/3) * sin(pi*(omega/180)), coorda - (taille/8) * cos(pi*(omega/180)), coordz - (taille/8) * sin(pi*(omega/180)) )  def drawgrdaig(coorda, coordz, taille, omega, can): #function draw minute hand, based on center coord , minutes.     pi = 3.141592     omega = (omega-15)*6     can.create_line(coorda + (taille/1.5) * cos(pi*(omega/180)), coordz + (taille/1.5) * sin(pi*(omega/180)), coorda - (taille/6) * cos(pi*(omega/180)), coordz - (taille/6) * sin(pi*(omega/180)))  def drawsecaig(coorda, coordz, taille, omega, can): #function draw hour hand     pi = 3.141592     omega = (omega-15) *6     can.create_line(coorda + (taille/1.5) * cos(pi*(omega/180)), coordz + (taille/1.5) * sin(pi*(omega/180)), coorda - (taille/6) * cos(pi*(omega/180)), coordz - (taille/6) * sin(pi*(omega/180)), fill = "red")  def fondhorloge(coorda, coordz, taille, can1):  #function drawing backgroud of clock     pi = 3.141592     drawcircle(coorda, coordz, taille + (taille/10), "grey6",can1) #drawing surrounding of clock     drawcircle(coorda, coordz, taille, "ivory3",can1)#backgroud     drawcircle(coorda, coordz, taille/80, "grey6",can1)#central point/needle articulation     can1.create_line(coorda + (taille - (taille/15)), coordz, coorda + (taille - (taille/5)), coordz) #drawing n/s/e/w decorativ hour position     can1.create_line(coorda, coordz + (taille - (taille/15)), coorda, coordz + (taille - (taille/5)))     can1.create_line(coorda - (taille - (taille/15)), coordz, coorda - (taille - (taille/5)), coordz)     can1.create_line(coorda, coordz - (taille - (taille/15)), coorda, coordz - (taille - (taille/5)))      #here, 4*2 line defined position of 8 intermediate line between n/s/e/w decorativ line.     can1.create_line(coorda + (taille/1.05) * cos(pi*(30/180)), coordz + (taille/1.05) * sin(pi*(30/180)), coorda + (taille/1.20) * cos(pi*(30/180)), coordz + (taille/1.20) * sin(pi*(30/180)))     can1.create_line(coorda + (taille/1.05) * cos(pi*(60/180)), coordz + (taille/1.05) * sin(pi*(60/180)), coorda + (taille/1.20) * cos(pi*(60/180)), coordz + (taille/1.20) * sin(pi*(60/180)))      can1.create_line(coorda - (taille/1.05) * cos(pi*(30/180)), coordz - (taille/1.05) * sin(pi*(30/180)), coorda - (taille/1.20) * cos(pi*(30/180)), coordz - (taille/1.20) * sin(pi*(30/180)))     can1.create_line(coorda - (taille/1.05) * cos(pi*(60/180)), coordz - (taille/1.05) * sin(pi*(60/180)), coorda - (taille/1.20) * cos(pi*(60/180)), coordz - (taille/1.20) * sin(pi*(60/180)))      can1.create_line(coorda + (taille/1.05) * cos(pi*(30/180)), coordz - (taille/1.05) * sin(pi*(30/180)), coorda + (taille/1.20) * cos(pi*(30/180)), coordz - (taille/1.20) * sin(pi*(30/180)))     can1.create_line(coorda + (taille/1.05) * cos(pi*(60/180)), coordz - (taille/1.05) * sin(pi*(60/180)), coorda + (taille/1.20) * cos(pi*(60/180)), coordz - (taille/1.20) * sin(pi*(60/180)))      can1.create_line(coorda - (taille/1.05) * cos(pi*(30/180)), coordz + (taille/1.05) * sin(pi*(30/180)), coorda - (taille/1.20) * cos(pi*(30/180)), coordz + (taille/1.20) * sin(pi*(30/180)))     can1.create_line(coorda - (taille/1.05) * cos(pi*(60/180)), coordz + (taille/1.05) * sin(pi*(60/180)), coorda - (taille/1.20) * cos(pi*(60/180)), coordz + (taille/1.20) * sin(pi*(60/180)))  #principle function (here problem starts)  def horloge1(gamma, pi, epsylon):# draw clock center position x/x = gamma/pi , radius = epsylon      fondhorloge(gamma, pi, epsylon, can1)# extracting time value     patate = gmtime()     heure = patate[3] + 1  # "+1" changing time english time french time :p     minute = patate[4]     seconde = patate[5]      print(heure, minute, seconde) # simple test watch programm doing (run it, , you'll see, test done tausend time per second, why think problem here.)     drawpetaig(gamma, pi, epsylon, minute, can1)     drawgrdaig(gamma, pi, epsylon, minute, can1)     drawsecaig(gamma, pi, epsylon, seconde, can1)     fen1.after(1000, horloge1(250, 250, 200))  #execution of main function  fen1 = tk() can1 = canvas(fen1, bg="white", height=500, width=500) can1.pack()  horloge1(250, 250, 200)  fen1.mainloop() fen1.destroy() 

the problem in principle function, horloge1() calls while trying arrange called again after specified 1 second delay. occurs in last line:

fen1.after(1000, horloge1(250, 250, 200)) 

a simple way prevent this, use instead:

fen1.after(1000, lambda: horloge1(250, 250, 200)) 

what create anonymous function using lambda expression . function created accepts/requires no arguments, , call horloge1(250, 250, 200) when it's called. that function passed fen1.after() second argument.

the notable thing doing not call horloge1() in process. second argument .after() should callable — such function or bound method — rather results of calling one, doing.

after making change, should see moving clock hands on it:

screenshot of analog clock displayed in tkinter window


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -