How to insert INSERT current time into login widget

Current code:

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()

The code above is an extract from my actual code, the problem is that when I try to insert the current time into the input widget, it displays as a decimal, but it displays fine in the console. Why is this?

Here is a screenshot:

enter image description here

I am using python 2.7.5

+4
source share
1 answer

You rewrite the variable Timewith the Entry widget, so put it in another variable, for example 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')

instead, do the following:

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)

Output:

enter image description here

+1
source

All Articles