473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamically displaying the time using Tkinter Label

440 256MB
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
8 31636
bartonc
6,596 Expert 4TB
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
psbasha
440 256MB
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
6,596 Expert 4TB
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
psbasha
440 256MB
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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
psbasha
440 256MB
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
6,596 Expert 4TB
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

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

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
2
by: Jeffrey Barish | last post by:
Is there a way to fill the values in an OptionMenu dynamically? I need something like the add_command method from Menu. Is there a better way to implement a pull-down list? -- Jeffrey Barish
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: npaulus | last post by:
Hi, I am trying to dynamically add user controls on to my web form but for some reason my form isnt displaying the user control. form1.cs: using System; using System.Drawing; using...
6
by: Dustan | last post by:
Nobody answered last time. I guess they wanted me to give it a shot. Well, here is how I download the image (it's a class method): def download_image(self):...
2
by: sj | last post by:
I am just learning to use Tkinter and am having problems displaying image files. I am able to display an image using tutorials (such as http://www.daniweb.com/code/snippet296.html) But when I try...
4
by: MartinRinehart | last post by:
Everything I've read about Tkinter says you create your window and then call its mainloop() method. But that's not really true. This is enough to launch a default window from the console: ...
1
by: viv1tyagi | last post by:
Hi everyone ! ! ! I'm just a month old in the world of Python and trying to develop an application using Tkinter in which a new window pops out on a particular button press.The code for this new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.