1

I'm trying to do a timer function in Tkinter python, in which I want to call one method consecutively in timer events.

My program is plain sub class, it doesn't contain root or master since I kept master class separate so I am struggling to write the after function, is it possible to write the after function without root? Because calling tk will display the unwanted tk window, I just want to see the timer event output only in my Python shell, is it possible?

class App():

    def __init__ (self): 

        self.timer_count = 0

        for Test in range(4):
           self.update_clock()


    def update_clock(self):

        self.timer_count+= 1
        print self.timer_count

        self.after(100, self.execute)  # Is it possible?
        #                                Can timer work without any root instance?
        #
        #self.root.after(100, self.update_clock)


App_Obj = App()
user3666197
  • 1
  • 6
  • 50
  • 92
John R
  • 119
  • 1
  • 4
  • 14
  • 1
    See : http://stackoverflow.com/questions/2400262/code-a-timer-in-a-python-gui-in-tkinter – Arthur Vaïsse Aug 26 '14 at 09:37
  • It'd be more useful if you described what is *apt*, the posts you've looked at that aren't *apt* and ultimately what you're trying to do and anything you've tried so far. Otherwise, it's unclear how to even approach helping you in a useful way. – Jon Clements Aug 26 '14 at 09:38
  • The description is calling a function from another function, the call back function should print or execute the statements in timer events – John R Aug 26 '14 at 09:44
  • @JohnR On your post-Edit clarification / question: **Yes**, however, `.after()` method has to have a Tkinter-widget association ( it is Tkinter method :o) ). **Do not worry and construct a Tkinter `root` instance ( to use it's `.after()` et al weaponry for scheduling ) + instruct `root.lower()` to hide it from Desktop** – user3666197 Aug 26 '14 at 12:30
  • When you say "in a Tkinter python program" we all assume that you're creating a Tkinter program. This implies one or more windows being created. If you aren't creating any windows, you aren't creating a Tkinter program. If you aren't creating windows, you can't use `after`. – Bryan Oakley Aug 26 '14 at 14:00
  • 1
    @Brayan, I am confused with your comment!, see I am creating TKinter program only but I have a main GUI class which will invoke my window, rest all classes I am keeping for support functionality. Here with your example I am facing error due to root tk window conflict. So I want only timer function in my subclass, don't want to invoke another window. In that case lower function helped me to an extent! not completely because still that window minimized down side. If you have any other knowledge to work on python timer without tk support, please help me out! Kindly avoid your duplicate marking – John R Aug 26 '14 at 14:19

2 Answers2

0

Tkinter can do, but remember, scheduling works in a different manner

Tkinter is a GUI-oriented framework, with a lot power built-in.

( Python per-se allows you to design things independently of Tkinter. )

Tkinter-side timer-based event(s) can be set, however as said above, user-side control of timer is avoided ( no reasonable near-real-time system will allow user to de-stabilise, the less to block, the flow of code-execution ... )

So. The Tkinter scheduling tools are basically these:

aTkScheduledEVENTid = <aTkRootWidget>.after( msecsAfter, aFun2Bcalled = None, *args )
# use
# for deterministic / set wait-time

aTkDeferredEVENTid  = <aTkRootWidget>.after_idle( aFun2Bcalled = None, *args )
# use
# for non-deterministic / deferred till <SIG_IDLE> state of the GUI control-loop

<aTkRootWidget>.after_cancel( { aTkScheduledEVENTid | aTkDeferredEVENTid } )
# use
# upon a need to **cancel**/re-schedule a set execution

The solo-call magic

A scheduled function call is executed only once, so it is rather common to repeat the scheduling task again "inside" the called function, to re-instate the registration for a next timer-based function call.

Arthur has posted above a link to a neat, Bryan Oakley's, code-snippet.

Adding a trick can help you read the real plasticity of the Tkinter timing under real load.

( Some platforms do not show time-resolution under [msec]-s )

class App():                            # Bryan Oakley, in http://stackoverflow.com/a/2401181/3666197
    def __init__( self ):
        self.root = tk.Tk()
        self.label = tk.Label( text = "init" )
        self.label.pack()
        self.update_clock()             # inital call to set a scheduled execution
        self.root.lower()               # OP.Edit: hide the Tk-window, as wanted
        self.root.mainloop()

    def update_clock( self ):
        # \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
        #
        # DEMO to show real plasticity of the Tkinter scheduler timing(s)
        #
        print time.time()               # show real activation timestamp w [msecs]
        #
        # /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
        now = time.strftime( "%H:%M:%S" )
        self.label.configure( text = now )
        self.root.after( 1000,         # re-instate next scheduled call, theoretically after given delay
                         self.update_clock
                         )
user3666197
  • 1
  • 6
  • 50
  • 92
0
import time

time.clock() #To start timing

#Your code here

timing = time.clock() #Returns a float ex. 0.05
                      #this is the time from start to finish in ms
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61