python 无需点击按钮即可更改时间

crcmnpdw  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(96)

只要微调框中的时间不变,下面的代码就能正常工作。我想要做的是设置休息时间。第一次它工作得很好,但之后如果我改变微调框中的值,系统将检查新值,而不是单击按钮时微调框中的值。

from tkinter import *
from tkinter import ttk
from datetime import datetime

root = Tk()
root.title("Fusion Calculator")

first_break_time_label = Label(root, text='1st Break:')
first_break_time_label.grid(row=0, column=0)

first_break_time_hour = Spinbox(root, from_=0, to=23, format='%02.0f')
first_break_time_hour.grid(row=0, column=1)

first_break_time_minute = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_minute.grid(row=0, column=2)

first_break_time_second = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_second.grid(row=0, column=3)

loop_active = False  # global variable set to false

def check_breaks():   # new loop function that runs set_breaks and reschedules
    set_breaks()      # for 1 second later.
    root.after(1000, check_breaks)

def set_breaks():
    check_break = datetime.now().strftime("%H:%M:%S")

    first_break_time_value = f'{first_break_time_hour.get()}:{first_break_time_minute.get()}:{first_break_time_second.get()}'

    if check_break == first_break_time_value:
        popup_first_break = Toplevel(root)
        popup_first_break.title('Official Break')
        popup_first_break_title_bar = Label(popup_first_break, text='OFFICIAL BREAK', fg='Red')
        popup_first_break_title_bar.pack()
    else:
        pass

    global loop_active
    if not loop_active:   #  This will only run when loop_active is false
        loop_active = True   # Set loop_active to True so it doesn't run again
        check_breaks()

set_breaks_button = ttk.Button(root, text='Set', command=set_breaks)
set_breaks_button.grid(row=1, column=3)

root.mainloop()

我试着用鼠标点击绑定按钮,这样它就可以创建一个数组,并将值放入数组中。但是我很难处理事件。

nqwrtyyt

nqwrtyyt1#

快速简单的方法是创建一个字典,其中包含初始设置为None的每个中断值,然后在第一次按下按钮时填充字典,同时启动循环,然后让循环在每次迭代时检查字典的中断时间,并将按钮命令设置为按下时更新字典的函数。
例如:

from tkinter import *
from tkinter import ttk
from datetime import datetime

root = Tk()
root.title("Fusion Calculator")

first_break_time_label = Label(root, text='1st Break:')
first_break_time_label.grid(row=0, column=0)

first_break_time_hour = Spinbox(root, from_=0, to=23, format='%02.0f')
first_break_time_hour.grid(row=0, column=1)

first_break_time_minute = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_minute.grid(row=0, column=2)

first_break_time_second = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_second.grid(row=0, column=3)

loop_active = False  # global variable set to false

breaks = {
    1: None
}

def check_breaks():   # new loop function that runs set_breaks and reschedules
    set_breaks()      # for 1 second later.
    root.after(1000, check_breaks)

def update_break():
    first_break_time_value = f'{first_break_time_hour.get()}:{first_break_time_minute.get()}:{first_break_time_second.get()}'
    breaks[1] = first_break_time_value
    set_breaks()

def set_breaks():
    check_break = datetime.now().strftime("%H:%M:%S")
    if check_break == breaks[1]:
        popup_first_break = Toplevel(root)
        popup_first_break.title('Official Break')
        popup_first_break_title_bar = Label(popup_first_break, text='OFFICIAL BREAK', fg='Red')
        popup_first_break_title_bar.pack()
    global loop_active
    if not loop_active:   #  This will only run when loop_active is false
        loop_active = True   # Set loop_active to True so it doesn't run again
        check_breaks()

set_breaks_button = ttk.Button(root, text='Set', command=update_break)
set_breaks_button.grid(row=1, column=3)

root.mainloop()

更好的选择可能是在程序开始时启动定时循环,这样你就可以摆脱循环的活动变量和额外的检查函数。
就像这样:

from tkinter import *
from tkinter import ttk
from datetime import datetime

root = Tk()
root.title("Fusion Calculator")

first_break_time_label = Label(root, text='1st Break:')
first_break_time_label.grid(row=0, column=0)

first_break_time_hour = Spinbox(root, from_=0, to=23, format='%02.0f')
first_break_time_hour.grid(row=0, column=1)

first_break_time_minute = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_minute.grid(row=0, column=2)

first_break_time_second = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_second.grid(row=0, column=3)

breaks = {
    1: None
}

def update_break():
    first_break_time_value = f'{first_break_time_hour.get()}:{first_break_time_minute.get()}:{first_break_time_second.get()}'
    breaks[1] = first_break_time_value

def check_breaks():
    check_break = datetime.now().strftime("%H:%M:%S")
    if check_break == breaks[1]:
        popup_first_break = Toplevel(root)
        popup_first_break.title('Official Break')
        popup_first_break_title_bar = Label(popup_first_break, text='OFFICIAL BREAK', fg='Red')
        popup_first_break_title_bar.pack()
    root.after(1000, check_breaks)

check_breaks()
set_breaks_button = ttk.Button(root, text='Set', command=update_break)
set_breaks_button.grid(row=1, column=3)

root.mainloop()

相关问题