Tkinter中while循环的替代方法

我正在尝试发出警报:警报应在预设时间响起。没有tkinter,我拥有的代码完全可以正常工作,但是使用tkinter则完全不起作用。 在原始代码中有一个while循环,我注意到并阅读到它无法与tkinter结合使用。我决定摆脱循环,但是现在当我输入时间后,闹钟立即响起。

我已经读过有关使用r.after(time_ms,function())的信息。但是我不知道如何在我的代码中正确实现它。我已经读到它必须在函数的末尾并且在r.mainloop()之前,但是我无法使其正常工作。有人能帮帮我吗?非常感谢你! 我已经阅读了很多有关此的内容或stackoverflow的帖子,但是我没有足够的经验来使它适用于这些答案。 :(

{% if not request.user.is_authenticated %}
wangruiyue 回答:Tkinter中while循环的替代方法

使用函数并在每个“指定的时间间隔”内为该函数注册一个回调,而不是while True:循环。这是在下面的代码中完成的:

def wait_for_alarm():
    after = r.after(1000,wait_for_alarm)  # 1000 refers to 1 second
    if time.localtime().tm_hour == hr and time.localtime().tm_min == mn and time.localtime().tm_sec == sc:
        print(message)
        alarm_Sound()
        mixer.music.play()
,

首先,您应该使用全局变量来保留Entry中的值,并稍后在检查时间的函数中使用它们。没有警报时间时,该函数应使用after重新运行。它还应该跳过after()以停止检查时间。

def display_output():
    global int_hour
    global int_minute
    global int_second

    hour = entry_hour.get()
    int_hour = int(hour)

    minute = entry_minute.get()
    int_minute = int(minute)

    second = entry_second.get()
    int_second = int(second)

    text_label['text'] = 'Alarm has been set for {}:{}:{}.'.format(hour,minute,second)
    # start checking time    
    check_time()


def check_time():

    dt = time.localtime()

    if (dt.tm_hour == int_hour and dt.tm_min == int_minute and dt.tm_sec == int_second):
        alarm_sound()
        mixer.music.play()
    else:
        r.after(1000,check_time) # check it again after 1000ms but only if it doesn't play sound

代码将需要其他全局变量来控制在停止警报以及尝试再次设置警报时是否正在运行警报。


import time
from datetime import datetime,date
import tkinter as tk
from pygame import mixer

# --- functions ---

def alarm_sound():
    global running_alarm

    running_alarm = True

    mixer.music.load('Wecker-sound.mp3')

def end():
    global running_alarm

    if running_alarm:
        running_alarm = False
        mixer.music.stop()
        text_label['text'] = ''

def display_output():
    global int_hour
    global int_minute
    global int_second

    hour = entry_hour.get()
    int_hour = int(hour)

    minute = entry_minute.get()
    int_minute = int(minute)

    second = entry_second.get()
    int_second = int(second)

    confirmation_message = ('Alarm has been set for {}:{}:{}.'.format(hour,second))

    text_label['text'] = confirmation_message

    check_time()


def check_time():

    dt = time.localtime()

    if (dt.tm_hour == int_hour and dt.tm_min == int_minute and dt.tm_sec == int_second):
        checking_time = False # stop l
        alarm_sound()
        mixer.music.play()
    else:
        r.after(1000,check_time)

# --- main ---

running_alarm = False # default value at start

mixer.init()


r = tk.Tk()
r.title('Smart Watch')

message = tk.Label(r,text='What is the alarm message you\'d like to receive when the alarm goes off?',font=('Roboto-regular',12),fg='black')
message.pack()

entry_message = tk.Entry(r,width=45)
entry_message.pack()

enter_hour = tk.Label(r,text='Enter the hour for the alarm to go off: ',fg='black')
enter_hour.pack()

entry_hour = tk.Entry(r,width=10)
entry_hour.pack()

enter_minute = tk.Label(r,text='Enter the minute for the alarm to go off: ',fg='black')
enter_minute.pack()

entry_minute = tk.Entry(r,width=10)
entry_minute.pack()

enter_second = tk.Label(r,text='Enter the second for the alarm to go off: ',fg='black')
enter_second.pack()

entry_second = tk.Entry(r,width=10)
entry_second.pack()

text_label = tk.Label(r,12,'bold'),fg='tomato')
text_label.pack()

submit_button = tk.Button(r,text='Submit',fg='black',width=30,height=2,relief='groove',cursor='hand2',command=display_output)
submit_button.pack()

snooze_button = tk.Button(r,text='Snooze alarm',command=end)
snooze_button.pack()

r.mainloop()
本文链接:https://www.f2er.com/3118539.html

大家都在问