tkinter 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 problem

 
0
  #1
Oct 9th, 2008
hi guys am stuck with my project.. i want to make the car stop when the lights are red and then move when its green. im providing you my code hoping you can help me.. 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, 80, 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 = 85
  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.  
  59.  
  60. def green_light(self):
  61. # imagine a square box with
  62. # upper left corner coordinates x1, y1
  63. # lower right corner coordinates x2, y2
  64. # and sides of length span for each circle
  65. span = 12
  66. x1 = 173
  67. y1 = 85
  68. x2 = x1 + span
  69. y2 = y1 + span
  70. #canvas.create_oval(x1, y1, x2, y2, fill='red')
  71. #canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow')
  72. canvas.create_oval(x1, y1+38, x2, y2+38, fill='green')
  73.  
  74.  
  75.  
  76.  
  77. root=tk.Tk()
  78. root.title("Traffic Simulation")
  79. canvas = tk.Canvas(root, width=1000, height=400, bg="#FFFFFF")
  80. clock = ElapsedTimeClock(root, font=('times', 12, 'bold'), bg='white')
  81. clock.pack(fill=BOTH, expand=1)
  82.  
  83. canvas.pack()
  84. light = TrafficLight()
  85. y=light.red_light()
  86. z=light.green_light
  87. root.after(10000,z)
  88.  
  89.  
  90.  
  91.  
  92.  
  93. # make roads
  94. l1=canvas.create_line(140,150,200,150,
  95. 200,150,200,50, width=3)
  96. l2=canvas.create_line(250,150,250,50,
  97. 250,150,374,150,
  98. 374,150,374,50, width=3)
  99. l3=canvas.create_line(424,150,424,50,
  100. 424,150,542,150,
  101. 542,150,542,50, width=3)
  102. l4=canvas.create_line(592,150,592,50,
  103. 592,150,674,150,
  104. 674,150,674,50, width=3)
  105. l5=canvas.create_line(724,150,724,50,
  106. 724,150,850,150, width=3)
  107.  
  108. l6=canvas.create_line(140,200,200,200,
  109. 200,200,200,300, width=3)
  110. l7=canvas.create_line(250,200,250,300,
  111. 250,200,344,200,
  112. 344,200,344,300, width=3)
  113. l8=canvas.create_line(394,200,394,300,
  114. 394,200,542,200,
  115. 542,200,542,300, width=3)
  116. l9=canvas.create_line(592,200,592,300,
  117. 592,200,674,200,
  118. 674,200,674,300, width=3)
  119. l10=canvas.create_line(724,200,724,300,
  120. 724,200,850,200, width=3)
  121.  
  122. # color roads
  123. rect=canvas.create_rectangle(140,152,850,198, fill="#999999", outline="#999999")
  124. rect1=canvas.create_rectangle(202,50,248,300, fill="#999999", outline="#999999")
  125. rect2=canvas.create_rectangle(376,50,422,152, fill="#999999", outline="#999999")
  126. rect3=canvas.create_rectangle(346,198,392,300, fill="#999999", outline="#999999")
  127. rect4=canvas.create_rectangle(544,52,590,300, fill="#999999", outline="#999999")
  128. rect5=canvas.create_rectangle(676,52,722,300, fill="#999999", outline="#999999")
  129.  
  130.  
  131. # lines on road
  132. l11=canvas.create_line(140,175,850,175, width=2, fill="White")
  133. l12=canvas.create_line(225,50,225,150,248,150, width=2, fill="White")
  134. l13=canvas.create_line(202,200,248,200, width=2, fill="White")
  135. l14=canvas.create_line(225,200,225,300, width=2, fill="White")
  136. l15=canvas.create_line(399,50,399,150,422,150, width=2, fill="White")
  137. l16=canvas.create_line(346,200,392,200, width=2, fill="White")
  138. l17=canvas.create_line(369,200,369,300, width=2, fill="White")
  139. l18=canvas.create_line(567,50,567,150,590,150, width=2, fill="White")
  140. l19=canvas.create_line(699,50,699,150,722,150, width=2, fill="White")
  141. l20=canvas.create_line(544,200,590,200, width=2, fill="White")
  142. l21=canvas.create_line(567,200,567,300, width=2, fill="White")
  143. l22=canvas.create_line(676,200,722,200, width=2, fill="White")
  144. l23=canvas.create_line(699,200,699,300, width=2, fill="White")
  145. l24=canvas.create_line(200,152,200,175, width=2, fill="White")
  146. l25=canvas.create_line(374,152,374,175, width=2, fill="White")
  147. l26=canvas.create_line(542,152,542,175, width=2, fill="White")
  148. l27=canvas.create_line(674,152,674,175, width=2, fill="White")
  149. l28=canvas.create_line(724,198,724,175, width=2, fill="White")
  150. l29=canvas.create_line(592,198,592,175, width=2, fill="White")
  151. l30=canvas.create_line(394,198,394,175, width=2, fill="White")
  152. l31=canvas.create_line(250,198,250,175, width=2, fill="White")
  153.  
  154.  
  155.  
  156. # create car
  157. #car = canvas.create_rectangle(20, 155, 40, 170, outline='blue', fill='blue')
  158. #car=canvas.create_oval(22,158,30,166, outline='blue', fill='blue')
  159. #car1=canvas.create_oval(922,178,930,186, outline='blue', fill='blue')
  160. #car2=canvas.create_oval(722,188,730,196, outline='yellow', fill='yellow')
  161.  
  162. car1 = canvas.create_rectangle(20, 155, 40, 170, outline='blue', fill='blue')
  163. # move car
  164.  
  165.  
  166. for x in range(200):
  167. y= x = 5
  168.  
  169. time.sleep(0.025)
  170. canvas.move(car1,x,0)
  171. canvas.update()
  172. #canvas.event_generate(car)
  173.  
  174.  
  175.  
  176. #start = time.time()
  177. root.mainloop()
  178. light.mainloop()
Last edited by vegaseat; Oct 14th, 2008 at 12:20 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,056
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 298
woooee woooee is offline Offline
Veteran Poster

Re: tkinter problem

 
0
  #2
Oct 9th, 2008
You can use a variable named something like self.red_light_on. Set it to True when the light changes to red, and False on a change to green or yellow. Then
  1. if not self.red_light_on:
  2. canvas.move(car1,x,0)
  3. canvas.update()
Reply With Quote Quick reply to this message  
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

Re: tkinter problem

 
0
  #3
Oct 14th, 2008
i've tried several pieces of code but still it doesn't work. can u please fix it for me? thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: Luckymeera is an unknown quantity at this point 
Solved Threads: 0
Luckymeera Luckymeera is offline Offline
Newbie Poster

Re: tkinter problem

 
0
  #4
Oct 16th, 2008
can anyone plz fix this problem because am having a similar problem with my codes. looking forward for a reply. thanks in advance..
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC