如何在Entry Widget中插入当前时间

时间:2013-12-16 12:10:56

标签: python tkinter

当前代码:

from Tkinter import *
import time

Time = time.strftime('%H:%M%p')
print Time

root = Tk()
root.option_add('*Font', 'courier 12')
root.option_add('*Background', 'grey')
root.configure(bg = 'grey')

w, h = 203, 50
x, y = (root.winfo_screenwidth()/2) - (w/2), (root.winfo_screenheight()/2) - (h/2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

Time = Entry(root, relief = RIDGE, bd = 5, width = 16, cursor = 'plus', fg = 'red', selectforeground = 'red', selectbackground = 'black')
Time.place(x = 0, y = 0)
Time.insert(0, Time)

root.title('Time') 
root.mainloop()

上面的代码是我实际代码的摘录,问题是当我尝试在条目小部件中插入当前时间时,它显示为小数但在控制台中显示正常。为什么是这样?

这是一个屏幕截图:

enter image description here

我正在使用python 2.7.5

1 个答案:

答案 0 :(得分:1)

您正在使用Entry小部件覆盖变量Time,因此将其放在另一个变量中,例如time

#You initialize it:
Time = time.strftime('%H:%M%p')
# Then you overwrite it:
Time = Entry(root, relief = RIDGE, bd = 5, width = 16, cursor = 'plus', fg = 'red', selectforeground = 'red', selectbackground = 'black')

改为:

time = time.strftime('%H:%M%p')
print time

Time = Entry(root, relief = RIDGE, bd = 5, width = 16, cursor = 'plus', fg = 'red', selectforeground = 'red', selectbackground = 'black')
Time.place(x = 0, y = 0)
Time.insert(0, time)

输出:

enter image description here

相关问题