0

I have a Python code that runs query from SQL server and gives back the output whenever the run query button is clicked in Tkinter. The issue is sometimes the query takes about 10-20 min to finish and I wouldn't know exactly how much time did the query take to complete.

The code below is a sample of printing on screen in Tkinter.

from tkinter import *    

def command(d):
    print(d)

a = Tk()
b = []

for c in range(0, 5):
    x = Button(a, text=c, command=lambda j=c: command(j)))
    x.pack()
    b.append(x)

a.mainloop()

So I am thinking of adding like a status bar to print the time when the button was clicked and record time again when the process is finished after the display of message box.

epsilon
  • 2,849
  • 16
  • 23
Ahmed
  • 25
  • 1
  • 7
  • Possible duplicate of [How do I get time of a Python program's execution?](https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution) – stovfl Feb 06 '19 at 14:48
  • Are you asking how to get the time, or asking how to update a label? Both of those have been asked and answered many times on stackoverflow. What _specific_ part of the problem are you struggling with? – Bryan Oakley Feb 06 '19 at 14:51

1 Answers1

2

You can use datetime module to get the current time and assign a callback for the button to change it on the display like this (datetime.datetime.now() gets the current time, label widget is associated with a variable in a way that when the variable changes, that label changes as well):

import tkinter as tk
import datetime


def command():
    global time
    time.set(str(datetime.datetime.now()))


root = tk.Tk()
time = tk.StringVar()
time.set('0')
button = tk.Button(root, text='Print time', command=command)
button.pack()
label = tk.Label(root, textvariable=time)
label.pack()

root.mainloop()