我是python中的新手,即时尝试让用户随时进入并将其与当时进行比较。当我尝试下面的编码时,时钟的结果没有显示确切的时间。而是打印不同的如果你能提供帮助,我们表示赞赏。
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
答案 0 :(得分:1)
如果我说得对,那么问题在于您尝试将Label
转换为字符串表示形式:print(clock)
。虽然你想获得它的属性,指的是它的文本内容,即'text'
。所以你需要做的是使用以下任一方式获取属性:
print(clock['text'])
或
print(clock.cget('text'))
另外,这里:
def tick2():
if clock == time_entry.get():
print("Correct")
else:
print("Wrong")
root.destroy()
你可能想要比较字符串,所以它是:
if clock['text'] == time_entry.get():
print("Correct")
else:
print("Wrong")
root.destroy()
P.S。 time.strftime('%H: %M')
会给你时间空间,例如'11:11'而不是'11:11',所以在比较这些字符串时,对于包含和不包含空格的字符串,它将返回false。