我已經嘗試修改 checkTimer() 中的初始 if 陳述句,這似乎是問題所在,但我在過去幾個小時內嘗試的幾種解決方案都沒有解決這個問題,它們都沒有效果,導致計時器不準確,或引起其他問題。
計時器的初始時間已修改為一分鐘以進行測驗。
# IMPORTS #
import tkinter as tk
from tkinter.font import *
from playsound import playsound
import os
from itertools import cycle
import json
from tkinter import PhotoImage
import time
from ctypes import Structure, windll, c_uint, sizeof, byref
class LASTINPUTINFO(Structure):
_fields_ = [
('cbSize', c_uint),
('dwTime', c_uint),
]
def get_idle_duration():
lastInputInfo = LASTINPUTINFO()
lastInputInfo.cbSize = sizeof(lastInputInfo)
windll.user32.GetLastInputInfo(byref(lastInputInfo))
millis = windll.kernel32.GetTickCount() - lastInputInfo.dwTime
return millis / 1000.0
t = time.localtime()
current_hrs = time.strftime("%H")
current_minutes = time.strftime("%M")
current_secs = time.strftime("%S")
current_hrs = int(current_hrs)
current_minutes = int(current_minutes)
current_secs = int(current_secs)
print(current_secs)
print(current_minutes)
print(current_hrs)
imagesCreated = 0
inARow = 0
# VARIABLES #
# minutes = cycle((25, 5, 25, 5, 25, 5, 25, 5, 25, 30))
minutes = cycle((1, 1))
notificationText = ""
directory = os.getcwd()
isTimerDone = False
onBreak = False
pomodoros = 0
jsonFile = open("Real-Time Drowsiness Detection OpenCV Python Source Code/VALUE.json", 'r ')
data = json.load(jsonFile)
drowsy = 0
workTime = 25
# FUNCTIONS #
# TKINTER #
window = tk.Tk()
window.title("Focus-Ware")
bigFontRegular = Font(
family="Helvetica",
size=35,
)
titleFont = Font(
family="Helvetica",
size=50
)
buttonimage = PhotoImage(file="Images/start.png")
bigFontItalic = Font(
family="Helvetica",
size=35,
slant="italic"
)
regularFontItalic = Font(
family="Helvetica",
size=25,
slant="italic"
)
window.geometry("510x600")
title = tk.Label(
text="Focus-Ware",
font=titleFont,
foreground="black",
)
timerTitle = tk.Label(
text="Timer:",
font=bigFontItalic,
foreground="black"
)
timer = tk.Label(
text="Not Started",
font=bigFontRegular,
foreground="black"
)
reminderTitle = tk.Label(
text=notificationText,
font=regularFontItalic,
foreground="black"
)
reminder = tk.Label(
text="Last Notification:",
font=bigFontItalic,
foreground="black"
)
def doNothing():
return "nothing"
pomodoroDone = False
target_hours = current_hrs
target_minutes = current_minutes
target_secs = current_secs
periods = 0
def setTimer():
global target_minutes
global target_hours
global current_hrs
global current_minutes
global onBreak
global periods
global current_secs
t = time.localtime()
current_hrs = time.strftime("%H")
current_minutes = time.strftime("%M")
current_hrs = int(current_hrs)
current_minutes = int(current_minutes)
current_secs = int(current_secs)
timer.config(text="In Progress")
window.after(10000, checkTimer)
def checkTimer():
global jsonFile
global target_minutes
global target_hours
global current_hrs
global current_minutes
global onBreak
global periods
global drowsy
global current_secs
global target_secs
jsonFile = open("Real-Time Drowsiness Detection OpenCV Python Source Code/VALUE.json", 'r ')
drowsy = int(data["VALUE"])
timer.config(text="In Progress")
t = time.localtime()
current_hrs = time.strftime("%H")
current_minutes = time.strftime("%M")
current_secs = time.strftime("%S")
current_hrs = int(current_hrs)
current_minutes = int(current_minutes)
current_secs = int(current_secs)
if get_idle_duration() >= 600:
reminderTitle.config(text="Make sure to work!")
playsound(f'{os.getcwd()}/Audio/GETTOWORK.wav')
if drowsy == 1:
playsound(f'{os.getcwd()}/Audio/wakeup.wav')
if current_minutes == target_minutes and (target_secs == current_secs or target_secs < current_secs):
onBreak = not onBreak
timer.config(text="Completed")
playsound(f'{os.getcwd()}/Audio/notification.mp3')
if onBreak:
if periods == 4:
target_minutes = current_minutes 30
if target_minutes >= 60:
target_minutes -= 60
target_hours = 1
periods = 0
else:
target_minutes = current_minutes 5
if target_minutes >= 60:
target_minutes -= 60
target_hours = 1
else:
target_minutes = current_minutes 1
target_secs = current_secs
if target_minutes >= 60:
target_minutes -= 60
target_hours = 1
periods = 1
else:
timer.config(text="In Progress")
window.after(1000, checkTimer)
#
#
# def timerDone():
# global onBreak
# global isTimerDone
# global workTime
# global drowsy
# drowsy = int(data["VALUE"])
# if drowsy == 1:
# workTime = 20
# else:
# workTime = 25
# isTimerDone = True
# reminderTitle.config(text="The timer is done!")
# onBreak = not (onBreak)
# if onBreak:
# reminderTitle.config(text="Go on break!")
# else:
# reminderTitle.config(text="Get to work!")
# window.after(2000, bruhwhatbruh)
# if onBreak:
# timer.config(text=f'Completed, take a break!')
# else:
# timer.config(text=f'Completed, get to work!')
# playsound(f'{os.getcwd()}/Audio/notification.mp3')
#
#
# def bruhwhatbruh():
# drowsy = int(data["VALUE"])
# if drowsy == 1:
# reminderTitle.config(text="You look tired!")
#
#
# def betweenTimers():
# global inARow
# global workTime
# drowsy = int(data["VALUE"])
# if drowsy == 1:
# reminderTitle("You look tired!")
# playsound(f'{os.getcwd()}/Audio/notification.mp3')
# inARow = 1
# workTime = 20
# else:
# workTime = 25
# if inARow == 3:
# inARow = 0
# reminderTitle("Please take a break!")
# playsound(f'{os.getcwd()}/Audio/notification.mp3')
#
#
# def setSubTimers(wkt):
# for i in range(2):
# if i == 0 or i == 1:
# pass
# else:
# window.after((workTime * wkt) round(2 / i) * 60 * 1000, betweenTimers)
#
#
# def setTimer(mins=25):
# timer.config(text="In Progress")
# setSubTimers(0)
# window.after(2 * 60 * 1000, timerDone)
# timer.config(text="In Progress")
# window.after(workTime 5 * 60 * 1000, timerDone)
# timer.config(text="In Progress")
# setSubTimers(1)
# window.after(workTime 5 workTime * 60 * 1000, timerDone)
# timer.config(text="In Progress")
# window.after(workTime 5 workTime 5 * 60 * 1000, timerDone)
# timer.config(text="In Progress")
# setSubTimers(2)
# window.after(workTime 5 workTime 5 workTime * 60 * 1000, timerDone)
# timer.config(text="In Progress")
# window.after(workTime 5 workTime 5 workTime 5 * 60 * 1000, timerDone)
# timer.config(text="In Progress")
# setSubTimers(3)
# window.after(workTime 5 workTime 5 workTime 5 workTime * 60 * 1000, timerDone)
# timer.config(text="In Progress")
# window.after(workTime 5 workTime 5 workTime 5 workTime 5 * 60 * 1000, timerDone)
# timer.config(text="In Progress")
# setSubTimers(4)
# window.after(workTime 5 workTime 5 workTime 5 workTime 5 workTime * 60 * 1000, timerDone)
# timer.config(text="In Progress")
# window.after(workTime 5 workTime 5 workTime 5 workTime 5 workTime 30 * 60 * 1000, timerDone)
#
def resetTimer():
timer.config(text="Not Started")
def reminder_f():
reminderTitle.config(text="Remember to pay attention!")
playsound(f'{os.getcwd()}/Audio/notification.mp3')
Start = tk.Button(
command=setTimer,
text="Start Pomodoro",
image=buttonimage
)
Start.config(height=100, width=100)
# DRIVER CODE #
# LOOP #
title.pack()
reminder.pack(pady=40)
reminderTitle.pack()
Start.pack()
timerTitle.pack(pady=40)
timer.pack()
window.mainloop()
uj5u.com熱心網友回復:
您不需要重新發明輪子……使用 python 的 datetime 模塊來跟蹤和比較時間。
from datetime import datetime, timedelta
target_time = datetime.now() timedelta(minutes=30)
def check_time():
if datetime.now() < target_time:
print("not yet ...")
else:
print("TIME'S UP!")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/338687.html
上一篇:Tkinter字串變數
下一篇:使用Python更新MySQL
