第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

慕慕森 2023-05-09 16:00:36
您好,我的開關(guān)不起作用,工作代碼:當(dāng)我按下 xa 時(shí),45 秒計(jì)時(shí)器啟動(dòng)。45 秒后計(jì)時(shí)器消失,然后當(dāng)我再次按 x 時(shí)沒有任何反應(yīng)。我想要實(shí)現(xiàn)的目標(biāo):45 秒后我想再次單擊 x 以再次啟動(dòng)計(jì)時(shí)器并繼續(xù)執(zhí)行此操作: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貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊

在您的代碼中,tkinter 循環(huán)阻塞了主循環(huán)。當(dāng)計(jì)時(shí)器完成時(shí),您需要退出 tk 循環(huán)。您還需要僅在啟動(dòng)計(jì)時(shí)器時(shí)才啟動(dòng) tk 循環(huán),否則 tk 循環(huán)將永遠(yuǎn)不會(huì)退出。


這是工作代碼:


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



查看完整回答
反對(duì) 回復(fù) 2023-05-09
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)