[Tutor] Clock in tkinter?

Mic o0MB0o at hotmail.se
Thu Nov 17 20:17:53 CET 2011


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 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.

from tkinter import*
import time
import os

class Window(Frame):
    def __init__(self,master):
        super (Window,self).__init__(master)
        self.grid()
        self.the_time=''
        self.create_widgets()
        self.update_time()
        

    def create_widgets(self):

        #Create a hello Button:
        self.hellobttn=Button(self, text="Hey")
        self.hellobttn.grid(row=0, column=1)

        #Create a label that displays time.
        self.display_time=Label(self, text=self.the_time)
        self.display_time.grid(row=1, column=1)

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




root=Tk()
root.title("Test")
root.geometry("200x200")
app=Window(root)
root.mainloop()    




I get no error while doing this, and the program works fine.

I have another question.

Say that I have a class and I want to make 100 objects. 
Then it could look like this:


#How to shorten this?


#Create the class.
class Chairs(object):
    def __init__(self, age, weight):
        self.age=age
        self.weight=weight

    def __str__(self):

        rep=self.age+self.weight
        return rep


#Create the objects
chair1=Chairs("10","20")
chair2=Chairs("10","20")
chair3=Chairs("10","20")
chair4=Chairs("10","20")
chair5=Chairs("10","20")
chair6=Chairs("10","20")
chair7=Chairs("10","20")
chair8=Chairs("10","20")
chair9=Chairs("10","20")
#And so on


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?


Right now, it feels like a lot of my code in my other programs could be made dramatically shorter.


Thanks!


        

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


More information about the Tutor mailing list