Sample Python Timer program

放肆的年华 提交于 2021-02-10 15:53:42

问题


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()

回答1:


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
                         )



回答2:


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


来源:https://stackoverflow.com/questions/25502484/sample-python-timer-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!