ARJhe / tkinter_tutorial

tutorial of tkinter
0 stars 0 forks source link

lamda expression to create instance and automatic init. #2

Open ARJhe opened 5 years ago

ARJhe commented 5 years ago

wrong way

start = tk.Button(root ,text='start', command= lambda : Clock.timer)

class Clock():
    def __init__(self):
        self.init_time = t.time()
        self.timer()

    def timer(self):
        recall = Timer(1.0,self.timer)
        time_elape = t.time() - self.init_time
        label['text']= self.formater(time_elape)
# get error

init_time is not defined

correct way

# lambda create instance of Clock() automatic initalize init time
start = tk.Button(root ,text='start', command= lambda : Clock())

class Clock():
    def __init__(self):
        self.init_time = t.time()
        self.timer()

    def timer(self):
        recall = Timer(1.0,self.timer)
        time_elape = t.time() - self.init_time
        label['text']= self.formater(time_elape)
ARJhe commented 5 years ago

Reference


>>> class B():
・・・     count = 1
・・・     def __init__(self):
・・・         self.count += 1
・・・     def show_it(self):
・・・         print("I have ",self.count," object.")
・・・
 
>>> bird = B()
>>> B.count
1
>>> bird.count
2
>>> bird.show_it()
I have 2 object.