TypeError:after_cancel()接受2个位置参数,但给出了3个

时间:2020-01-19 13:57:55

标签: python python-3.x tkinter

我正在使用Tkinter制作节拍器,并使用screen.after()来播放滴答声,但是当我尝试停止循环以便可以更改每秒节拍时,我得到了错误,TypeError: after_cancel() takes 2 positional arguments but 3 were given即使我确实给出了2个参数,但没有给出3个。 这是我的代码:

from tkinter import *
from time import sleep
import os
import sys
times1 = 0
times2 = 1
def quit_():
    screen.destroy()
    sys.exit()
def run():
    global times1
    global times2
    times1 += 1
    bpm = bpm_.get()
    entry.delete(0, END)
    bpm = int(bpm)
    bpm = bpm/60
    bpm = 1/bpm
    def run_():
        global times1
        global times2
        sleep(bpm)
        os.system("afplay metronome.wav&")
        screen.after(1,run_)
        if times1 > times2:
            screen.after_cancel(1,run_)
            times1 = 0
            times2 = 1
    run_()
def main():
    global bpm_
    global entry
    bpm_ = StringVar()
    Label(screen, text="").pack()
    Label(screen, text = "enter a bpm").pack()
    entry = Entry(screen, textvariable=bpm_)
    entry.pack()
    Label(screen, text="").pack()
    Button(screen, text = "enter", command = run).pack()
    Label(screen, text="").pack()
    Button(screen, text = "quit", command = quit_).pack()
screen = Tk()
main()
screen.mainloop()

0 个答案:

没有答案