| | |
tkinter loop problem
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 38
Reputation:
Solved Threads: 0
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..
python Syntax (Toggle Plain Text)
import Tkinter as tk import time import sys import datetime as dt from Tkinter import * class ElapsedTimeClock(Label): def __init__(self, parent, *args, **kwargs): Label.__init__(self, parent, *args, **kwargs) self.lastTime = "" t = time.localtime() self.zeroTime = dt.timedelta(hours=t[3], minutes=t[4], seconds=t[5]) self.tick() def tick(self): # get the current local time from the PC now = dt.datetime(1, 1, 1).now() elapsedTime = now - self.zeroTime time2 = elapsedTime.strftime('%H:%M:%S') # if time string has changed, update it if time2 != self.lastTime: self.lastTime = time2 self.config(text=time2) # calls itself every 200 milliseconds # to update the time display as needed # could use >200 ms, but display gets jerky self.after(200, self.tick) class TrafficLight(tk.Frame): """inherits tk.Frame""" def __init__(self, parent=None): tk.Frame.__init__(self, parent, bg='green') #self.grid() # create a canvas to draw on #self.cv = tk.Canvas(self, width=260, height=280, bg='white') #self.cv.grid() self.make_widgets() #self.red_light() def make_widgets(self): canvas.create_rectangle(168, 90, 190, 140) def red_light(self): # imagine a square box with # upper left corner coordinates x1, y1 # lower right corner coordinates x2, y2 # and sides of length span for each circle span = 12 x1 = 173 y1 = 95 x2 = x1 + span y2 = y1 + span canvas.create_oval(x1, y1, x2, y2, fill='red') #canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow') #canvas.create_oval(x1, y1+38, x2, y2+38, fill='green') canvas.create_oval(x1, y1+28, x2, y2+28, fill='white') def green_light(self): # imagine a square box with # upper left corner coordinates x1, y1 # lower right corner coordinates x2, y2 # and sides of length span for each circle span = 12 x1 = 173 y1 = 95 x2 = x1 + span y2 = y1 + span #canvas.create_oval(x1, y1, x2, y2, fill='red') #canvas.create_oval(x1, y1+19, x2, y2+19, fill='yellow') canvas.create_oval(x1, y1+28, x2, y2+28, fill='green') canvas.create_oval(x1, y1, x2, y2, fill='white') root=tk.Tk() root.title("Traffic Simulation") canvas = tk.Canvas(root, width=1000, height=400, bg="#FFFFFF") clock = ElapsedTimeClock(root, font=('times', 12, 'bold'), bg='white') clock.pack(fill=BOTH, expand=1) canvas.pack() light = TrafficLight() y=light.red_light() z=light.green_light m = light.red_light root.after(5000,z) root.after(10000,m) #root.after(10000,m) #canvas.update() #start = time.time() root.mainloop() light.mainloop()
Last edited by vegaseat; Oct 14th, 2008 at 12:17 pm. Reason: added code tags
Please use code tags
[code=python]
*Your code goes between these tags
[/code]
Infinite loop=
Initialization goes before the loop.
[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.
![]() |
Similar Threads
- Multiple tasks in Tkinter (Python)
- GUI Python - A Guessing Game (Python)
- Roulette program simulation problem (Python)
- Positioning again. (Python)
- adding numbers in tk (Python)
- Tkinter GUI Issues: (Python)
- Something similar to def in Tkinter (Python)
- Problem with find (Python)
Other Threads in the Python Forum
- Previous Thread: Beginning: Starting Python
- Next Thread: What is the advantage.....
Views: 493 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






