[Tutor] Clock in tkinter?

Wayne Werner waynejwerner at gmail.com
Thu Nov 17 20:30:52 CET 2011

On Thu, Nov 17, 2011 at 1:17 PM, Mic <o0MB0o at hotmail.se> wrote:

>   I have now worked to stop using the global scope and instead put my
> prior global variable into
> the constructor in the class. I believe that I have managed to do that now.
> Do you believe that this is correctly done?
> #Trying putting the_time'' in constructor instead of
> #putting it in the global scope to shorten code.
> <snip>
   def update_time(self):
        self.display_time.config(text=time.strftime('%H:%M:%S'), font='40')
>        self.after(20, self.update_time)

Since you're using the shorter version of the function you no longer need
self.the_time, so you can go ahead and remove that variable.

> I have another question.

It would have been fine (and even preferred) to create a new email here
with a title something like "How do I shorten this code?"

> Say that I have a class and I want to make 100 objects.
> Then it could look like this:
> <snip>
> class Chairs(object):
> <snip code>

> #Create the objects
> chair1=Chairs("10","20")
> chair2=Chairs("10","20")
> chair3=Chairs("10","20")

> How do I shorten this? I have thought of using a for sling. I have looked
> in my programming
> book and on the internet, but I don’t know how to make this shorter. The
> arguements (“10”, “20”)
> should be the same for every object, which should make it easier than if
> they were different each time?

If you ever write a line of code more than once, it's a good sign that you
have what's called a code smell. This example is very smelly code ;)

What you should do instead is have a collection of chairs:

chairs = []
for _ in range(100): # the underscore `_` indicates that you don't care
about the value

You can do that even shorter with a list comprehension, but I'll leave that
exercise up to you :)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111117/528cb79b/attachment-0001.html>

More information about the Tutor mailing list