Search code examples
pythoncanvastkintereasygui

How do I automatically quit a canvas while keeping my program running?


I currently have this code:

import easygui, random, time, math, sys
from Tkinter import *

root=Tk()
root.geometry('800x800')
c=Canvas(root, width=800, height=800)
c.pack()
done=False
x=1
while not done:
    #do something
    root.update()
    x=x+1
    if x>=5:
        c.destroy()
    else:
        done=False

y=easygui.buttonbox("The program should continue",
                    choices=['Yes','No'])

I would like the program to continue after the 'if x>5:' branch, onto the easygui portion of my code. I looked this question up, and found c.destroy. Unfortunately, it ends my code entirely, so the easygui button box doesn't even appear. Does anybody know how to delete the Tkinter canvas, and go on with the program. Would using ttk instead of easygui solve my problem? Or is there a simple fix in continuing my code with the same method I'm using?


Solution

  • it ends my code entirely, so the easygui button box doesn't even appear.

    No it doesn't. It just removes the window, and then your code is still stuck in the loop forever. In addition to c.destroy(), you need to stop the while loop by adding a break:

    if x>=5:
        c.destroy()
        break