如何使按钮取消.after命令

时间:2019-05-24 03:55:45

标签: python python-3.x tkinter

我目前有一个显示新屏幕的功能(称为A),然后倒计时10秒。 10秒后,它将带用户到新屏幕(B)。如果用户在10秒钟前单击屏幕“ A”上的按钮,我想使.after命令不执行。

class MainGate3(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        background_image = tk.PhotoImage(file="Sylvanas.png")  # Displays whats in the file
        background_label = tk.Label(self, image=background_image)  # Makes tk.Label display the background_image
        background_label.place(x=0, y=0, relwidth=1, relheight=1)  # Adjusts the height/width

        background_label.image = background_image  # Recognises the label as an image

        def gate():
            controller.show_frame("MainGate4")
            button1.after(10000, lambda:controller.show_frame("MainMenu"))

        button1 = tk.Button(self, text="Commence Battle", bg="black", fg="white", height=2, width=20,
                            command=gate)
        button1.place(x="712", y="433")

class MainGate4(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        background_image = tk.PhotoImage(file="Sylvanas1.png")  # Displays whats in the file
        background_label = tk.Label(self, image=background_image)  # Makes tk.Label display the background_image
        background_label.place(x=0, y=0, relwidth=1, relheight=1)  # Adjusts the height/width

        background_label.image = background_image  # Recognises the label as an image

        button1 = tk.Button(self, text="Parry Right", bg="black", fg="white", height=2, width=20,
                            command=lambda: controller.show_frame("MainGate3"))
        button2 = tk.Button(self, text="Parry Left", bg="black", fg="white", height=2, width=20,
                            command=lambda: controller.show_frame("MainGate3"))
        button3 = tk.Button(self, text="Dodge", bg="black", fg="white", height=2, width=20,
                            command=lambda: controller.show_frame("MainGate3"))
        button1.place(x="83", y="140")
        button2.place(x="83", y="240")
        button3.place(x="83", y="340")

1 个答案:

答案 0 :(得分:4)

malloc方法返回一个标识符。您以后可以使用标识符通过after方法取消。

after_cancel