tkinter loop problem

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 38
Reputation: harrykokil is an unknown quantity at this point 
Solved Threads: 0
harrykokil harrykokil is offline Offline
Light Poster

tkinter loop problem

 
0
  #1
Oct 14th, 2008
hi guys.. im having problems with my traffic lights. i want to execute the procesing of the traffic lights infinitely (i.e red to green to red continuously). im providing my code hoping to get a positive response soon. thanks..
  1. import Tkinter as tk
  2. import time
  3. import sys
  4. import datetime as dt
  5. from Tkinter import *
  6.  
  7. class ElapsedTimeClock(Label):
  8. def __init__(self, parent, *args, **kwargs):
  9. Label.__init__(self, parent, *args, **kwargs)
  10. self.lastTime = ""
  11. t = time.localtime()
  12. self.zeroTime = dt.timedelta(hours=t[3], minutes=t[4], seconds=t[5])
  13. self.tick()
  14.  
  15. def tick(self):
  16. # get the current local time from the PC
  17. now = dt.datetime(1, 1, 1).now()
  18. elapsedTime = now - self.zeroTime
  19. time2 = elapsedTime.strftime('%H:%M:%S')
  20. # if time string has changed, update it
  21. if time2 != self.lastTime:
  22. self.lastTime = time2
  23. self.config(text=time2)
  24. # calls itself every 200 milliseconds
  25. # to update the time display as needed
  26. # could use >200 ms, but display gets jerky
  27. self.after(200, self.tick)
  28.  
  29.  
  30. class TrafficLight(tk.Frame):
  31. """inherits tk.Frame"""
  32. def __init__(self, parent=None):
  33. tk.Frame.__init__(self, parent, bg='green')
  34. #self.grid()
  35. # create a canvas to draw on
  36. #self.cv = tk.Canvas(self, width=260, height=280, bg='white')
  37. #self.cv.grid()
  38.  
  39. self.make_widgets()
  40. #self.red_light()
  41.  
  42. def make_widgets(self):
  43. canvas.create_rectangle(168, 90, 190, 140)
  44.  
  45. def red_light(self):
  46. # imagine a square box with
  47. # upper left corner coordinates x1, y1
  48. # lower right corner coordinates x2, y2
  49. # and sides of length span for each circle
  50. span = 12
  51. x1 = 173
  52. y1 = 95
  53. x2 = x1 + span
  54. y2 = y1 + span
  55. canvas.create_oval(x1, y1, x2, y2, fill='red')
  56. #canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow')
  57. #canvas.create_oval(x1, y1+38, x2, y2+38, fill='green')
  58. canvas.create_oval(x1, y1+28, x2, y2+28, fill='white')
  59.  
  60.  
  61. def green_light(self):
  62. # imagine a square box with
  63. # upper left corner coordinates x1, y1
  64. # lower right corner coordinates x2, y2
  65. # and sides of length span for each circle
  66. span = 12
  67. x1 = 173
  68. y1 = 95
  69. x2 = x1 + span
  70. y2 = y1 + span
  71. #canvas.create_oval(x1, y1, x2, y2, fill='red')
  72. #canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow')
  73. canvas.create_oval(x1, y1+28, x2, y2+28, fill='green')
  74. canvas.create_oval(x1, y1, x2, y2, fill='white')
  75.  
  76.  
  77.  
  78. root=tk.Tk()
  79. root.title("Traffic Simulation")
  80. canvas = tk.Canvas(root, width=1000, height=400, bg="#FFFFFF")
  81. clock = ElapsedTimeClock(root, font=('times', 12, 'bold'), bg='white')
  82. clock.pack(fill=BOTH, expand=1)
  83.  
  84. canvas.pack()
  85. light = TrafficLight()
  86. y=light.red_light()
  87. z=light.green_light
  88. m = light.red_light
  89. root.after(5000,z)
  90. root.after(10000,m)
  91.  
  92. #root.after(10000,m)
  93. #canvas.update()
  94.  
  95. #start = time.time()
  96. root.mainloop()
  97. light.mainloop()
Last edited by vegaseat; Oct 14th, 2008 at 12:17 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 269
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: tkinter loop problem

 
0
  #2
Oct 14th, 2008
Please use code tags
[code=python]
*Your code goes between these tags
[/code]

Infinite loop= while True: Put the things you want to repeat inside the loop.
Initialization goes before the loop.
Last edited by jlm699; Oct 14th, 2008 at 8:55 am.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: tkinter loop problem

 
0
  #3
Oct 16th, 2008
What is the error message you get?
Last edited by sneekula; Oct 16th, 2008 at 11:42 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 493 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC