亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

切換不工作的“鍵盤”Python 和 Tkinter

切換不工作的“鍵盤”Python 和 Tkinter

慕慕森 2023-05-09 16:00:36
您好,我的開關不起作用,工作代碼:當我按下 xa 時,45 秒計時器啟動。45 秒后計時器消失,然后當我再次按 x 時沒有任何反應。我想要實現的目標:45 秒后我想再次單擊 x 以再次啟動計時器并繼續執行此操作:from tkinter import *import keyboardfrom playsound import playsoundroot = Tk()root.geometry("+0+0")root.overrideredirect(True)root.wm_attributes("-topmost", True)root.wm_attributes("-alpha", 0.01)root.resizable(0, 0)seconds = 45toggle_button = 'x'enabled = Falsedef 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 = Falsewhile 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()
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

在您的代碼中,tkinter 循環阻塞了主循環。當計時器完成時,您需要退出 tk 循環。您還需要僅在啟動計時器時才啟動 tk 循環,否則 tk 循環將永遠不會退出。


這是工作代碼:


import tkinter as tkr

import keyboard

from playsound import playsound


root = None

timer_display = None


root = 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 = 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)

        root.quit()  # exit tk root loop



def start_countdown():

    root.wm_attributes('-alpha', 0.7)

    countdown(seconds)


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()  # timer will exit this loop



查看完整回答
反對 回復 2023-05-09
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號