Embedded Real Time Clock

Does anyone know if it is possible to place a real time clock on a Base form? I’m not talking about the date/time field when a record is created. I am writing a Ham Radio logging database and I would like to have a clock showing UTC or Universal Time and maybe a few other time zones.

Enquiring minds want to know…

With Python is easy. Is importante start macro main from form.

import uno
import datetime
import threading

event = None


def run_in_thread(fn):
    def run(*k, **kw):
        t = threading.Thread(target=fn, args=k, kwargs=kw)
        t.start()
        return t
    return run


@run_in_thread
def start_clock():
    app.debug('Start...')
    global event
    event = threading.Event()

    doc = XSCRIPTCONTEXT.getDocument()
    form = doc.DrawPage.Forms[0]
    label = form['lbl_clock']

    while not event.wait(1):
        show_clock(label)
    return


def show_clock(label):
    now = datetime.datetime.now().replace(microsecond=0).time()
    label.Label = str(now)
    return


def stop_clock():
    global event
    event.set()
    app.debug('Stop...')
    return


def main(args=None):
    start_clock()
    return

foro

1 Like

I removed the app.debug statements and pass the form’s load event to the main routine, so it works withe stand-alone forms and embedded forms, with the code embedded in the Base document or in the global scope.
@stinsonpilot,

  • Open the Script folder in your profile directory.
  • Create a subfolder python.
  • Save the script code as a plain text file with suffix .py
  • Add a label control named lbl_clock to your data form.
  • Assign the main routine to the form’s event When loading
  • The form needs to be linked to some record set.

For arbitrary stand-alone Writer documents, use the original version with app.debug statements removed.

# coding: utf-8
from __future__ import unicode_literals
import uno
import datetime
import threading

event = None


def run_in_thread(fn):
    def run(*k, **kw):
        t = threading.Thread(target=fn, args=k, kwargs=kw)
        t.start()
        return t
    return run


@run_in_thread
def start_clock(form):
    global event
    event = threading.Event()
    label = form['lbl_clock']

    while not event.wait(1):
        show_clock(label)
    return


def show_clock(label):
    now = datetime.datetime.now().replace(microsecond=0).time()
    label.Label = str(now)
    return


def stop_clock():
    global event
    event.set()
    return


def main(ev):
    start_clock(ev.Source)
    return
1 Like

Thank you!

Note to future people searching for this solution. Both elamu and Vileroy both have a correct solution. I just filpped a coin to see who I would be mark for the answer to the question