切换不工作的“键盘”Python 和 Tkinter

您好,我的开关不起作用,


工作代码:当我按下 xa 时,45 秒计时器启动。45 秒后计时器消失,然后当我再次按 x 时没有任何反应。


我想要实现的目标:45 秒后我想再次单击 x 以再次启动计时器并继续执行此操作:


from tkinter import *

import keyboard

from playsound import playsound


root = Tk()


root.geometry("+0+0")

root.overrideredirect(True)

root.wm_attributes("-topmost", True)

root.wm_attributes("-alpha", 0.01)

root.resizable(0, 0)


seconds = 45


toggle_button = 'x'


enabled = False


def countdown(time):

    if time > 0:

        mins, secs = divmod(time, 60)


        def color_change(t_time):

            if t_time > 10:

                return 'green'

            elif 7 <= t_time <= 10:

                return 'yellow'

            elif t_time < 7:

                return 'red'


        timer_display.config(text="{:02d}:{:02d}".format(mins, secs),

                             fg=color_change(time)), root.after(1000, countdown, time - 1)

    else:

        root.wm_attributes('-alpha', 0.01)



def start_countdown():

    root.wm_attributes('-alpha', 0.7)

    countdown(seconds)



timer_display = Label(root, font=('Trebuchet MS', 30, 'bold'), bg='black')

timer_display.pack()


last_state = False



while True:

    key_down = keyboard.is_pressed(toggle_button)

    # If the toggle button is pressed, toggle the enabled value and print

    if key_down != last_state:

        last_state = key_down

        if last_state:

            enabled = True

            if enabled:

                start_countdown()

                print("Activated")

                playsound('count.mp3')

            else:

                start_countdown()

        root.mainloop()


慕慕森
浏览 102回答 1
1回答

qq_遁去的一_1

在您的代码中,tkinter 循环阻塞了主循环。当计时器完成时,您需要退出 tk 循环。您还需要仅在启动计时器时才启动 tk 循环,否则 tk 循环将永远不会退出。这是工作代码:import tkinter as tkrimport keyboardfrom playsound import playsoundroot = Nonetimer_display = Noneroot = tkr.Tk()root.geometry("+0+0")root.overrideredirect(True)root.wm_attributes("-topmost", True)root.wm_attributes("-alpha", 0.01)root.resizable(0, 0)timer_display = tkr.Label(root, font=('Trebuchet MS', 30, 'bold'), bg='black')timer_display.pack()seconds = 45toggle_button = 'x'enabled = Falsedef countdown(time):&nbsp; &nbsp; if time > 0:&nbsp; &nbsp; &nbsp; &nbsp; mins, secs = divmod(time, 60)&nbsp; &nbsp; &nbsp; &nbsp; def color_change(t_time):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if t_time > 10:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 'green'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif 7 <= t_time <= 10:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 'yellow'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif t_time < 7:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 'red'&nbsp; &nbsp; &nbsp; &nbsp; timer_display.config(text="{:02d}:{:02d}".format(mins, secs),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fg=color_change(time)), root.after(1000, countdown, time - 1)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; root.wm_attributes('-alpha', 0.01)&nbsp; &nbsp; &nbsp; &nbsp; root.quit()&nbsp; # exit tk root loopdef start_countdown():&nbsp; &nbsp; root.wm_attributes('-alpha', 0.7)&nbsp; &nbsp; countdown(seconds)last_state = Falsewhile True:&nbsp; &nbsp; key_down = keyboard.is_pressed(toggle_button)&nbsp; &nbsp; # If the toggle button is pressed, toggle the enabled value and print&nbsp; &nbsp; if key_down != last_state:&nbsp; &nbsp; &nbsp; &nbsp; last_state = key_down&nbsp; &nbsp; &nbsp; &nbsp; if last_state:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enabled = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if enabled:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start_countdown()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Activated")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playsound('count.mp3')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start_countdown()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root.mainloop()&nbsp; # timer will exit this loop
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python