nabeel
nabeel

Reputation: 427

Passing a function to a class in Python 3

well it doesn't shows any errors once i add self.methodToRun = function yet the function doesn't get called.(This is what happens

>>> func()
1
>>> ================================ RESTART ================================
>>> 
>>> tt = timer_tick(1,func)
>>> tt.start()
>>> )

here's the code

import time

def func():
    print('1')

class timer_tick:

    def __init__(self, num, function):
        self.delay = num
        self.methodToRun = function
        self.timercondition = False
    def start(self):
        timercondition = True
        self.timer()
    def timer(self):
        while self.timercondition:
            self.methodToRun()
            time.sleep(self.delay)

    def stop(self):
        timercondition = False

def method1():
    return 'hello world'

def method2(methodToRun):
    result = methodToRun()
    return result

well I discovered this while writing a stopwatch program, that a timer effect can be achieved by the tkinter.Tk.after() function. I was able to add control to stop,pause and reset the timer with it.

import tkinter
import random
import time

frame = tkinter.Tk()
frame.title("Stopwatch")
frame.geometry('500x300')

t='00:00.0'
helv30 = ('Helvetica', 30)
num = 0
timer = 1


def timerhand():
    global num,n
    num += 1
    formate(num)
def formate(num):
    global t,n
    if (get_seconds(num) >= 0 and get_seconds(num)<10):
     if (num%100)%10 == 0 and (num//600) < 10 :
       t ='0'+ str(get_minutes(num)) + ':'+'0' + str(get_seconds(num))+'.0'
     elif (num//600) < 10:
       t ='0'+ str(get_minutes(num)) + ':'+'0' + str(get_seconds(num))
     elif (num%100)%10 == 0:
       t =str(get_minutes(num)) + ':'+'0' +  str(get_seconds(num))+'.0'
     else:
       t = str(get_minutes(num)) + ':'+'0' + str(get_seconds(num))
    else:
     if (num%100)%10 == 0 and (num//600) < 10 :
       t ='0'+ str(get_minutes(num)) + ':' + str(get_seconds(num))+'.0'
     elif (num//600) < 10:
       t ='0'+ str(get_minutes(num)) + ':' + str(get_seconds(num))
     elif (num%100)%10 == 0:
       t =str(get_minutes(num)) + ':' +  str(get_seconds(num))+'.0'
     else:
       t = str(get_minutes(num)) + ':' + str(get_seconds(num))
def get_minutes(num):
    return (num//600)
def get_seconds(num):
    return (num%600)/10

def stop():
    global timer,t,num
    timer = 0
    t = '00:00.0'
    num = 0
def pause():
    global timer,t
    timer = 0

def start():
    global timer
    timer = 1
    clock()

canvas1 = tkinter.Canvas(frame,width = 300, height = 100,bg = 'black')
t_message = canvas1.create_text(150,50, text = t , fill = 'blue', font = helv30 )

b1= tkinter.Button(frame,text = "Stop",command = stop)
b1.pack(side ="bottom")

b2= tkinter.Button(frame,text = "Start",command = start)
b2.pack(side ="bottom")

b2= tkinter.Button(frame,text = "Pause",command = pause)
b2.pack(side ="bottom")


#here is the time function implementation

def clock():
    global canvas1,t_message
    timerhand()

    canvas1.itemconfig(t_message, state = 'hidden')
    t_message = canvas1.create_text(150,50, text = t , fill = 'blue', font = helv30 )
    canvas1.pack()    

    if timer == True:
        frame.after(100,clock)


clock()


canvas1.pack()


frame.mainloop()

so I was looking for ways to achieve this without the tkinter module

Upvotes: 1

Views: 87

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 121974

You are converting the function to a string:

self.methodToRun = str(function)

Therefore when you call it:

self.methodToRun()

You are trying to call a string, which won't work. You can just store the function like any other parameter:

self.methodToRun = function

And pass as you would any other argument:

tt = timer_tick(my_num, my_func)

You should probably also be storing the delay and timercondition as instance attributes:

def __init__(self, num, function):
    self.delay = num
    self.methodToRun = function
    self.timercondition = False

Edit: your updated version has two problems:

  1. You refer to timercondition, not self.timercondition, in start; and
  2. timer will run forever, as it never returns control to allow you to call stop.

The former is easy to solve, the latter much less so.

Upvotes: 4

Related Questions