By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,426 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,426 IT Pros & Developers. It's quick & easy.

Dynamically displaying the time using Tkinter Label

100+
P: 440
Hi,

Could anybody help me in displaying the time at some Corner of the dialog,when the application is running.

Display time on the dialog for the entire process.


Thanks
PSB
Apr 8 '07 #1
Share this Question
Share on Google+
8 Replies


bartonc
Expert 5K+
P: 6,596
Hi,

Could anybody help me in displaying the time at some Corner of the dialog,when the application is running.

Display time on the dialog for the entire process.


Thanks
PSB
Expand|Select|Wrap|Line Numbers
  1. # use Tkinter to show a digital clock
  2. # tested with Python24    vegaseat    10sep2006
  3.  
  4. from Tkinter import *
  5. import time
  6.  
  7. root = Tk()
  8. time1 = ''
  9. clock = Label(root, font=('times', 20, 'bold'), bg='green')
  10. clock.pack(fill=BOTH, expand=1)
  11.  
  12. def tick():
  13.     global time1
  14.     # get the current local time from the PC
  15.     time2 = time.strftime('%H:%M:%S')
  16.     # if time string has changed, update it
  17.     if time2 != time1:
  18.         time1 = time2
  19.         clock.config(text=time2)
  20.     # calls itself every 200 milliseconds
  21.     # to update the time display as needed
  22.     # could use >200 ms, but display gets jerky
  23.     clock.after(200, tick)
  24.  
  25. tick()
  26. root.mainloop(  )
Apr 8 '07 #2

100+
P: 440
Thanks for the reply.

Is it possible to display the duration of time taken for executing the application?

Say ,it should start with 00:00:00, and display the time taken for the aplication.


1) start time

2) Mainn function call

3) End time

In between start and end time ,the time duration has to be displayed.Any suggestions on this.

-PSB
Apr 8 '07 #3

bartonc
Expert 5K+
P: 6,596
Thanks for the reply.

Is it possible to display the duration of time taken for executing the application?

Say ,it should start with 00:00:00, and display the time taken for the aplication.


1) start time

2) Mainn function call

3) End time

In between start and end time ,the time duration has to be displayed.Any suggestions on this.

-PSB
47 seconds flat:
Expand|Select|Wrap|Line Numbers
  1. >>> import datetime as dt
  2. >>> import time
  3. >>> t = time.localtime()
  4. >>> t
  5. (2007, 4, 8, 2, 11, 19, 6, 98, 1)
  6. >>> zeroTime = dt.timedelta(hours=t[3], minutes=t[4], seconds=t[5])
  7. >>> now = dt.datetime(1, 1, 1).now()
  8. >>> now - zeroTime
  9. datetime.datetime(2007, 4, 8, 0, 0, 47, 324000)
  10. >>> 
Apr 8 '07 #4

100+
P: 440
As the application is running ,I would like to display the time duration taken dynamically on the dialog.

Say the appliation started with 00:00:00,as the application started processing the execution then the time display should show the following

00:00:01
00:00:02
......
00:00:47

Every second increment has to be shown in the display at particular location

Thanks
PSB
Apr 8 '07 #5

bartonc
Expert 5K+
P: 6,596
As the application is running ,I would like to display the time duration taken dynamically on the dialog.

Say the appliation started with 00:00:00,as the application started processing the execution then the time display should show the following

00:00:01
00:00:02
......
00:00:47

Every second increment has to be shown in the display at particular location

Thanks
PSB
Sorry, I left this out:
Expand|Select|Wrap|Line Numbers
  1. >>> elapsedTime = now - zeroTime
  2. >>> elapsedTime.strftime('%H:%M:%S')
  3. '10:29:09'
  4. >>> 
All the pieces are now here (10 hours, 29 minutes and 9 seconds later).
Apr 8 '07 #6

bartonc
Expert 5K+
P: 6,596
Sorry, I left this out:
Expand|Select|Wrap|Line Numbers
  1. >>> elapsedTime = now - zeroTime
  2. >>> elapsedTime.strftime('%H:%M:%S')
  3. '10:29:09'
  4. >>> 
All the pieces are now here (10 hours, 29 minutes and 9 seconds later).
I've put all the pieces together into a subclass of Label. Simple add this widget to you layout as you would a Tk.Label. Time starts when the widget is created.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import Tk, Label, BOTH
  2. import time
  3. import datetime as dt
  4.  
  5.  
  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. if __name__ == "__main__":
  31.     root = Tk()
  32.     clock = ElapsedTimeClock(root, font=('times', 20, 'bold'), bg='green')
  33.     clock.pack(fill=BOTH, expand=1)
  34.     root.mainloop()
  35.  
  36.  
Apr 8 '07 #7

100+
P: 440
Thanks Barton,

But where should I have to call my application/main method ,where the time starts before the method call,and it should keep showing the time in mins/secs (some times hrs) untill the method has been executed and come out of the method and display the final time duration .

It should stop showing the time ,once it comes out of the main method of the application/ application executed.


-PSB
Apr 9 '07 #8

bartonc
Expert 5K+
P: 6,596
Thanks Barton,

But where should I have to call my application/main method ,where the time starts before the method call,and it should keep showing the time in mins/secs (some times hrs) untill the method has been executed and come out of the method and display the final time duration .

It should stop showing the time ,once it comes out of the main method of the application/ application executed.


-PSB
My thinking was that what ever method was being timed would own the GUI.
If that's not the case, remove
Expand|Select|Wrap|Line Numbers
  1. self.tick()
and call
Expand|Select|Wrap|Line Numbers
  1. myClock.tick()
when you want to start it. You can add a flag to turn the timer on and off in tick().
I'd also add GetElapsedTime() and ResetElapsedTime().
Apr 9 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.