0

What I was wondering is if it's possible to put a timer in my program so that like every 1 min. the program will update a list box with data?

class App():
def __init__(self):
    self.root = tk.Tk()
    self.label = Label(text="")
    self.label.pack()
    self.update_clock()
    self.root.mainloop()

def update_clock(self):
    now = time.strftime("%H:%M:%S")
    self.label.configure(text=now)

    # Put Arrivals in box===============================================================
    arrivallb.delete(0, END)
    now = dt.datetime.now()
    dte = now.strftime("%m-%d-%Y")

    conn = sqlite3.connect('reservation.db')
    c = conn.cursor()
    c.execute("SELECT * FROM reserve")
    records = c.fetchall()

    for record in records:
        if record[22] != "*":
            if record[8] == dte:
                arrivallb.insert(0, str(record[13]) + "  " + record[0] + ", " + record[1])


    self.root.after(10000, self.update_clock)

app=App()

2 Answers2

1

I figured it out. Below is the code that updates my list box. Thanks everyone for your input.

class App():
def __init__(self):
    self.root = tk.Tk()
    self.label = Label(text="")
    self.label.pack()
    self.update_clock()
    self.root.mainloop()

def update_clock(self):
    now = time.strftime("%H:%M:%S")
    self.label.configure(text=now)

    # Put Arrivals in box===============================================================
    arrivallb.delete(0, END)
    now = dt.datetime.now()
    dte = now.strftime("%m-%d-%Y")

    conn = sqlite3.connect('reservation.db')
    c = conn.cursor()
    c.execute("SELECT * FROM reserve")
    records = c.fetchall()

    for record in records:
        if record[22] != "*":
            if record[8] == dte:
                arrivallb.insert(0, str(record[13]) + "  " + record[0] + ", " + record[1])


    self.root.after(10000, self.update_clock)

app=App()

0

Python has a library called ascynio, which is part of the standard library. You can have two or more separate processes (coroutines) running with a long sleep in one of them. Here's a mock program that may or may not work but has the general idea in mind.
Warning: this code hasn't been tested

import asyncio

async def wait(): # you can try and use a while true here
    await asyncio.sleep(60)
    updateListBox()
    return

async def otherStuff():
    # code here: make sure it terminates at some point, unless you try the while true method from above
    return

async def main():
    while True:
        await asyncio.gather(
            otherStuff(),
            wait()
        )

asyncio.run(main())

Here's the asyncio documentation https://docs.python.org/3/library/asyncio-task.html

Luke-zhang-04
  • 782
  • 7
  • 11