Reputation: 13
im a new beginner in python and im trying to get the user enter any time and compare it with the time being.When i try the coding below.the result of the clock didnt show the exact time.instead it print the different value.Much appreciated if you can help.
from Tkinter import*
from time import sleep
import time
import serial
root=Tk()
time1 = ''
clock = Label(font=('times', 20, 'bold'), bg='pink',fg='red')
print(clock)
clock.pack(fill=X,expand=1)
label=Label(text="Add New Time")
time_entry = Entry()
label.pack()
time_entry.pack()
def tick():
global time1
time2 = time.strftime('%H: %M')
if time2 != time1:
time1 = time2
clock.config(text=time2)
clock.after(200, tick)
def tick2():
if clock == time_entry.get():
print("Correct")
else:
print("Wrong")
root.destroy()
button = Button(root,text="submit",command=tick2)
button.pack()
tick()
root.mainloop( )
the red which i rounded is the value that dont show the exact time
Upvotes: 1
Views: 122
Reputation: 6331
If i get you right, the problem is that you are trying to convert Label
to string representation here: print(clock)
. While you want to get it's attribute referring to it's text content, which is 'text'
. So what you need to do is get the attribute using either:
print(clock['text'])
or
print(clock.cget('text'))
Also, here:
def tick2():
if clock == time_entry.get():
print("Correct")
else:
print("Wrong")
root.destroy()
you probably want to compare strings, so it's:
if clock['text'] == time_entry.get():
print("Correct")
else:
print("Wrong")
root.destroy()
P.S. time.strftime('%H: %M')
will give you time with a space, e.g. '11: 11' and not '11:11', so when comparing strings like these, it will return false for strings with and without the space.
Upvotes: 1