gpt4 book ai didi

python - 将时间与值(value)进行比较

转载 作者:太空宇宙 更新时间:2023-11-03 21:00:28 25 4
gpt4 key购买 nike

我有来自 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')

我使用此代码通过 insrt() 函数的 returnspinbox 获取变量

        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 不能很好地配合它

        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:5now 变量是这样的 03:05 code> 所以它根本不匹配,因为那个零,我能做些什么来正确地编译它?这是完整的代码

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

最佳答案

我发现与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()

关于python - 将时间与值(value)进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55737691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com