示例 Python 计时器程序 [英] Sample Python Timer program

查看:34
本文介绍了示例 Python 计时器程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Tkinter python 中做一个定时器函数,我想在定时器事件中连续调用一个方法.

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

我的程序是普通的子类,它不包含 root 或 master,因为我将 master 类分开,所以我在努力编写 after 函数,是否可以在没有 root 的情况下编写 after 函数?因为调用 tk 会显示不需要的 tk 窗口,我只是想看到定时器 事件 输出 only 在我的Python shell,有可能吗?

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

推荐答案

Tkinter 可以做到,但请记住,调度的工作方式不同

Tkinter 是一个面向 GUI 的框架,内置了很多功能.

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

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

(Python 本身允许您独立于 Tkinter 进行设计.)

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

Tkinter-side timer-based event(s) 可以设置,但是如上所述,用户端对定时器的控制是避免(没有合理的近实时系统会允许用户不稳定,更少阻塞,代码执行流程......)

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

所以.Tkinter 调度工具基本上是这些:

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 在上面发布了一个链接,指向一个简洁的 Bryan Oakley 代码片段.

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

添加一个技巧可以帮助您了解 Tkinter timing 在实际负载下的真实可塑性.

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

(某些平台在 [msec]-s 下不显示时间分辨率)

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

这篇关于示例 Python 计时器程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆