我有一个来自Tkinter Spinbox的值
e1 = Spinbox(window, from_=0, to=23, width= 3, state = 'readonly')
e2 = Spinbox(window, from_=0, to=59, width= 3, state = 'readonly')
e3 = Spinbox(window, from_=0, to=23, width= 3, state = 'readonly')
e4 = Spinbox(window, from_=0, to=59, width= 3, state = 'readonly')
我要使用此代码通过spinbox
函数的insrt()
从return
到变量
time_func = insrt() #takes variables from Tkinter out
first = time_func[0]
first = int(first)
second = time_func[1]
second = int(second)
third = time_func[2]
third = int(third)
fourth = time_func[3]
fourth = int(fourth)
并将其插入到morning
函数的变量time
morning = time.strftime("%d:%d" %(first, second))
evening = time.strftime("%d:%d" %(third, fourth))
但是它返回的内容如下:“ 3:5”(3小时5分钟)
我想将其与time.time(now)
进行比较,我不想使用datetime
,因为我想在定义的日期进行比较,而datetime
不能很好地配合使用< / p>
now = time.strftime("%H:%M", time.localtime(time.time()))
我正在使用time_in_range
函数进行比较
def time_in_range(morning, evening, x):
if morning <= evening:
return morning <= x <= evening
else:
return morning <= x or x <= evening
timerange = time_in_range(morning, evening, now)
如果相同则返回True,否则返回False。
但是存在一个小问题,morning
像这样的3:5
,而now
变量是这样的03:05
,所以它根本不匹配,因为有零,我该怎么做才能正确呢?这是完整的代码
import os
import sys
import time
import datetime
from tkinter import *
from time import sleep
from datetime import date
# Function for Tkinter to associate variables
def insrt():
element_1 = e1.get()
element_2 = e2.get()
element_3 = e3.get()
element_4 = e4.get()
return (element_1, element_2, element_3, element_4)
# Function for closing Tkinter window with button
def close_window ():
window.destroy()
# Creating Tkinter first window and lines and labels
window = Tk()
window.title("監視システム")
Label(window, text=" スタート時間(時)").grid(row=2)
Label(window, text=" スタート時間(分)").grid(row=3)
Label(window, text=" 終わりの時間(時)").grid(row=4)
Label(window, text=" 終わりの時間(分)").grid(row=5)
e1 = Spinbox(window, from_=0, to=23, width= 3, state = 'readonly')
e2 = Spinbox(window, from_=0, to=59, width= 3, state = 'readonly')
e3 = Spinbox(window, from_=0, to=23, width= 3, state = 'readonly')
e4 = Spinbox(window, from_=0, to=59, width= 3, state = 'readonly')
e1.insert(2,"")
e2.insert(2,"")
e3.insert(2,"")
e4.insert(2,"")
e1.grid(row=2, column=1)
e2.grid(row=3, column=1)
e3.grid(row=4, column=1)
e4.grid(row=5, column=1)
# Tkinter buttons
Button(window, text=' スタート ', command=window.quit).grid(row=7, column=1, sticky=E, pady=4)
Button(window, text = " 閉じる ", command = close_window).grid(row=7, column=0, sticky=W, pady=4)
mainloop() # End of first Tkinter window
#setting used variables for main function
time_func = insrt() # Calling for the function (associated vars)
first = time_func[0] #Taking first number from time_func
first = int(first) #converting for the strftime
second = time_func[1]
second = int(second)
third = time_func[2]
third = int(third)
fourth = time_func[3]
fourth = int(fourth)
fifth = time_func[4]
six = time_func[5]
six = int(six)
now = time.strftime("%H:%M", time.localtime(time.time())) # time now
morning = time.strftime("%d:%d" %(first, second)) # start time for the time function(inserting value)
evening = time.strftime("%d:%d" %(third, fourth)) # end time for the time function(inserting value)
def time_in_range(morning, evening, x):
if morning <= evening:
return morning <= x <= evening
else:
return morning <= x or x <= evening
while True:
timerange = time_in_range(morning, evening, now)
if timerange != True:
print("Waiting for the right time")
sleep(200)
else:
print("do something else")
答案 0 :(得分:1)
我发现与datetime
模块相比更容易。您所需要做的就是通过datetime.now()
来获取当前时间,然后通过替换来构造您的早/晚时间。
def compare_time():
...
now = datetime.now()
morning = now.replace(hour=int(first),minute=int(second))
evening = now.replace(hour=int(third), minute=int(fourth))
然后您可以直接比较所有3个。
此外,如果您选择OOP方法,则可以大大缩短代码:
from datetime import datetime
from tkinter import *
class MyGui(Tk):
def __init__(self):
Tk.__init__(self)
self.title("監視システム")
self.boxes = []
labels = (" スタート時間(時)"," スタート時間(分)",
" 終わりの時間(時)"," 終わりの時間(分)")
for num, label in enumerate(labels,2):
s = Spinbox(self, from_=0, to=23 if not num%2 else 59, width=3, state='readonly')
Label(self,text=label).grid(row=num)
s.grid(row=num,column=1)
self.boxes.append(s)
# Tkinter buttons
Button(self, text=' Start ', command=self.compare_time).grid(row=7, column=1, sticky=E, pady=4) #スタート
Button(self, text=" 閉じる ", command=self.destroy).grid(row=7, column=0, sticky=W, pady=4)
def compare_time(self):
first, second, third, fourth = [i.get() for i in self.boxes]
now = datetime.now()
morning = now.replace(hour=int(first),minute=int(second))
evening = now.replace(hour=int(third), minute=int(fourth))
print (f"Current time: {now}, Morning time: {morning}, Evening time: {evening}")
# do the rest of your time_in_range stuff here
root = MyGui()
root.mainloop()