[Tutor] Clock in tkinter?

Wayne Werner waynejwerner at gmail.com
Wed Nov 16 17:17:46 CET 2011


On Wed, Nov 16, 2011 at 9:28 AM, Mic <o0MB0o at hotmail.se> wrote:

>   Hi!
> It is no homework, in fact am I working ahead of class. I have now, after
> five minutes thinking, solved my problem, but a new problem has risen.
> But to begin with, here is how I solved the problem:
>
>
>
> from tkinter import*
> import time
>
> the_time=''
>
>
>
> class Window(Frame):
>     def __init__(self,master):
>         super(Window,self).__init__(master)
>         self.grid()
>         self.create_widgets()
>
>
>     def create_widgets(self):
>
>         #Create a hello button:
>         hej=self.hellobttn=Button(self,text="Hey")
>         self.hellobttn.grid(row=0, column=0)
>
>         #Create a label that displays time:
>         self.display_time=Label(self, text=the_time)
>         self.display_time.grid(row=0, column=1)
>
>         def change_value_the_time():
>             global the_time
>             newtime = time.strftime('%H:%M:%S')
>             if newtime != the_time:
>                 the_time= newtime
>                 self.display_time.config(text=the_time, font="40")
>             self.display_time.after(20, change_value_the_time)
>

If you're going to put a function inside your class (since you're using
self in there, I'm sure that's what you meant to do), you should change it
to:

    def change_value_the_time(self):

and call it with

    self.display_time.after(20, self.change_value_the_time)

But your program also has unnecessary code. First, since you have a class
then instead of using a global, you should simply declare `self.the_time =
''` in your __init__ for your class.

Personally, I would have written the function more like this:

def update_time(self):
    self.display_time.config(text=time.strftime('%H:%M:%S'), font='40')
    self.after(20, self.update_time)

Then at the end of my __init__ function I would call self.update_time()

I'm not sure how that will work with your current setup, though.

I found some help on the internet about making a clock, although I had to
> modify a lot of the code to fit my own code and window.
> Now to my next question. Say that I want a text “Hi, how are you?” to be
> printed when the time passes 15:00:00 each day. How do I do that?
>
> At first I thought that I could just write an if statement. Like:
>
> if the_time>15:00:00:
>     print (“Hi, how are you?”)
>
> But it is obviously not working.
>

You can write one very similar to that. Take a look at the time.localtime
method.


>
> Thank you for your help! Another question, am I supposed to add
> tutor at python.org; as copy? You did that, right?
>

Yes - if you click "reply to all" in your email client, that will
automatically send it to tutor at python.org

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111116/93f55238/attachment.html>


More information about the Tutor mailing list