时钟的滴答声不能与倒数计时器同时工作

Clock's clicking does not work in simoultaneous with countdown timer

我正在使用 tkinter 和 winsound。 我想让声音和倒数计时器同时工作。 现在,一旦咔嗒声结束,计时器就会出现。

我看过一些使用 "after" 的倒数计时器示例。例如:self.after(1000, self.countdown)。但我同时需要两者。

import tkinter as tk
from tkinter import Tk
from nBackTools.NBackTools import *
from nBackTools.ZBack import *
#To play sounds
import winsound 
from winsound import *
import numpy as np


class NBack:

    def __init__(self, master):
        ##Title of the window
        self.master = master
        master.title("N-Back")


        ##It measures the screen size (width x height + x + y)
        ##The opened window will be based on the screen size
        master.geometry("{0}x{1}-0+0".format(master.winfo_screenwidth(), master.winfo_screenheight()))
        self.canvas = tk.Canvas(master, width=master.winfo_screenwidth(), height=master.winfo_screenheight(), \
                            borderwidth=0, highlightthickness=0, bg="grey")


        self.canvasWidth = master.winfo_screenwidth()
        self.canvasHeight =  master.winfo_screenheight()


        ##If removed, a white screen appears
        self.canvas.grid()


        """

        BREAK TIMER

        """


        self.play()

        self.canvas.create_text(((self.canvasWidth/2), (self.canvasHeight/2)-130), text="LET'S TAKE A BREAK!", font=(None, 90))
        self.display = tk.Label(master, textvariable="")
        self.display.config(foreground="red", background = "grey", font=(None, 70), text= "00:00")
        self.display.grid(row=0, column=0, columnspan=2)


    def play(self):
        return PlaySound('clock_ticking.wav', SND_FILENAME)


root = Tk()
my_gui = NBack(root)
root.mainloop()

同时做两件事叫做"asynchronous"。要在 winsound 中启用该模式,您需要 ASYNC 标志:

def play(self):
    PlaySound('clock_ticking.wav', SND_FILENAME | SND_ASYNC)

您仍然需要使用 after 才能使倒计时生效。