Как заставить кнопку отменить команду .after - PullRequest
0 голосов
/ 24 мая 2019

В настоящее время у меня есть функция, которая показывает новый экран (назовем его A), а затем выполняет обратный отсчет 10 секунд.Через 10 секунд пользователь переходит на новый экран (B).Я бы хотел, чтобы команда .after не выполнялась, если пользователь нажимает кнопку на экране «A» до истечения 10 секунд.

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 Ответ

4 голосов
/ 24 мая 2019

Метод after возвращает идентификатор.Вы можете использовать идентификатор для отмены методом after_cancel позже.

import tkinter as tk

class GUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.action = None
        tk.Button(self,text="Print in 10 sec",command=self.callback).pack()
        tk.Button(self, text="Cancel print", command=self.cancel_callback).pack()

    def callback(self):
        self.action = self.after(10000,lambda: print ("Hi"))

    def cancel_callback(self):
        if self.action:
            self.after_cancel(self.action)
            print ("Print cancelled")

root = GUI()
root.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...