时钟的点击不能与倒数计时器同时进行

时间:2019-02-11 22:30:32

标签: python-3.x tkinter winsound

我正在使用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()

1 个答案:

答案 0 :(得分:1)

一次执行2件事称为“异步”。要在winsound中启用该模式,您需要ASYNC标志:

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

您仍然需要使用after才能开始倒计时。