6

I am building an application in Tkinter with a custom window through overrideredirect. I have bound my self-designed X button to the function below. Closing the app using my button works fine, and it does fade out, but after a few seconds the window reappears, gets stuck in a loop (that's what it looks like) and crashes. It should just quit, which is what it did before I added the fadeout loop. Can someone tell me why the program reappears then crashes or offer a better alternative for a fadeout effect when closing the app (I know there are more sophisticated toolkits but I need to use Tkinter in this case)?

Thanks

def CloseApp(event):
if InProgress==False: #InProgress boolean defined elsewhere in program
    if tkMessageBox.askokcancel("Quit","Do you really wish to quit?"):
        n=1
        while n != 0:
            n -= 0.1
            QuizWindow.attributes("-alpha", n)
            time.sleep(0.02)                                  
        Window.destroy() #I've also tried using the quit() method, not that it would make a difference
else:
    if tkMessageBox.askokcancel("Quit"," If you quit now you will lose your progress and have to start again. Are you sure you want to quit?"):
        n=1
        while n != 0:
            n -= 0.1
            QuizWindow.attributes("-alpha", n)
            time.sleep(0.02)
        Window.destroy() 
2
  • 1
    If you are able to provide any screenshots or error codes (if there are any), then it may end up being easier for people to help you out. As it can then give a better indication of what the problem may be.
    – El Novice
    Commented Mar 18, 2014 at 21:41
  • There are no error codes. Saving the fuss of making a video, whilst the app is running, pressing the close button causes the window to fade out. Then 5 seconds later the window reappears and is unresponsive. Under Windows 7 it is a whiteout and on click Windows returns 'pythonw.exe is unresponsive'. Commented Mar 18, 2014 at 21:46

1 Answer 1

10

You have two problems. First, you should never do exact comparisons to floating point numbers. Floating point math is imprecise, and n may never actually be 0.0000000....

Second, you should never call time.sleep in a GUI program. If you want to run something every .02 seconds, use after.

Here's an example:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        b = tk.Button(self, text="Click to fade away", command=self.quit)
        b.pack()
        self.parent = parent

    def quit(self):
        self.fade_away()

    def fade_away(self):
        alpha = self.parent.attributes("-alpha")
        if alpha > 0:
            alpha -= .1
            self.parent.attributes("-alpha", alpha)
            self.after(100, self.fade_away)
        else:
            self.parent.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
3
  • 1
    Hi. Can you explain me about your code? I don't understand what self.parent.attributes("-alpha") is. Commented Jun 27, 2018 at 10:48
  • @HoseongJeon: self.parent is the root window. .attributes is a documented method of the window. "-alpha"- is the name of an attribute to change and alpha is the new value. Commented Jun 27, 2018 at 11:57
  • Perfecto! Just what I was looking for. Let's see if this is extendable to the tooltip "style" that I have in my on-going GUI development. Mr. Oakley, I notice that you are a primary respondent to Python related posts, are you the "man"?
    – Tony A
    Commented Jun 7, 2020 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.