Python Forum
Cannot use function with timer/clock
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot use function with timer/clock
#1
I have been working for days now trying to implement a timer, which would take 5 minutes in any other language. I have tried every tutorial in existence and none of them work. I am now on my last nerve and I just do not understand why calling a function in python is so difficult. Below is a snippet of code that demonstrates my problem.

In the example, the function runs fine when called from another function in the class.
Likewise, "self.updateDebugger" is another function (not shown) in the class and calling it seems to be just fine. But if I try to schedule the code using a clock, I get an error stating "AttributeError: 'float' object has no attribute 'updateDebugger'"

class MainWindow(App):

	def clockFunc(self):
		self.updateDebugger("data read @ " + datetime.datetime.now())
		print ("doing stuff")
	
    Clock.schedule_interval(clockFunc, .5)

if __name__ == '__main__':	
	MainWindow().run()
The error "AttributeError: 'float' object has no attribute 'updateDebugger'" I assume is referring to the clock not being able to pass an argument as a a float. Why the clock needs to do such a thing I have no idea. I also dont understand what this has to do with "self.updateDebugger" since this method has nothing to do with the clock. The clock is scheduling the clockFunc() function. If the code runs fine when being called then why can't the clock run the code?
Reply
#2
In order to use a method from a class outside of the class, you need to intestate a new class, and must pass any required initialization
arguments ('App' for this class).
You also have to return something meaningful from the class method, otherwise it will go out of scope as soon as the method is finished.
arguments passed to class
This code as written has never run, it would fail immediately:
  • Not instantiating class
  • Line 7 doesn't belong in class
  • Have not created instance of class MainWindow
  • Method clockFunc does not take any arguments, yet you are trying to pass .5
  • clockFunc never returns a value
Fix all of above and post new code.
I Strongly recommend reading up on classes, here's a quick and dirty tutorial that you can do in less than 10 minutes: https://www.learnpython.org/en/Classes_and_Objects
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using threading.Timer for function korenron 1 1,194 Dec-20-2022, 01:09 PM
Last Post: ndc85430
  Module 'time' has no attribute 'clock' Sophie 4 3,105 Jan-25-2022, 08:05 PM
Last Post: Sophie
  Clock\time calculation script Drone4four 3 1,462 Jan-21-2022, 03:44 PM
Last Post: ibreeden
  time.clock not functioning Oldman45 3 2,696 Apr-07-2021, 08:51 AM
Last Post: Oldman45
  email timer/rss feed timer ndiniz 1 2,080 Feb-02-2021, 07:18 PM
Last Post: nilamo
  Run a timer when a functions starts to see how long the function takes to complete Pedroski55 2 1,995 Apr-19-2020, 06:28 AM
Last Post: Pedroski55
  Help with Stopping a function after a timer SheeppOSU 0 1,928 Jan-28-2019, 10:13 PM
Last Post: SheeppOSU
  Pythonista script with iPhone's native Clock app Sharkman157 0 2,598 Sep-27-2018, 05:23 PM
Last Post: Sharkman157
  Is there a max value for threading.Timer function? whookie 1 3,962 Feb-03-2018, 09:51 PM
Last Post: ka06059
  Problem with 'Clock' functionality panoss 2 3,568 May-17-2017, 07:03 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020