如何在Tkinter窗口中单击按钮并在Tkinter窗口中显示按钮,打印时间?

时间:2019-02-06 14:29:08

标签: python tkinter

我有一个Python代码,该代码可从SQL Server运行查询,并在Tkinter中单击运行查询按钮时返回输出。 问题是查询有时需要大约10-20分钟才能完成,而我不知道查询完成需要多少时间。

下面的代码是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()

因此,我正在考虑添加一个状态栏,以显示单击按钮时的时间,并在显示消息框后完成该过程时再次记录时间。

1 个答案:

答案 0 :(得分:2)

您可以使用datetime模块获取当前时间,并为按钮分配一个回调以在显示器上进行更改,如下所示(datetime.datetime.now()获取当前时间,label小部件与变量相关联,当变量更改时,该标签也会更改):

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()
相关问题