1

I am new to Tkinter. I am trying to destroy the Toplevel window and it gets destroyed perfectly but nothing's running after that. The cursor just keeps on blinking in python shell as it happens while running an infinite loop.

Here's my code :

def error_msg(msg) :
    root1 = Tk.Toplevel()
    root1.attributes("-topmost", True)
    root1.title("Error")
    w1 = 230
    h1 = 100
    ws1 = root1.winfo_screenwidth()
    hs1 = root1.winfo_screenheight()
    x1 = (ws1/2) - (w1/2)
    y1 = (hs1/2) - (h1/2)
    root1.geometry('%dx%d+%d+%d' % (w1, h1, x1, y1))
    can1 = Tk.Canvas(root1,width = 230,height=100)
    can1.pack()
    im1 = Image.open("img.png")
    tkimage1 = ImageTk.PhotoImage(im1)
    Canvas_Image1 = can1.create_image(0,0, image=tkimage, anchor="nw")

    canvas_id1 = can1.create_text(15, 10, anchor="nw")
    can1.itemconfig(canvas_id1, text=msg)
    Tk.Button(root1, text='OK', command =root1.destroy).place(x=110,y=70)
    root1.mainloop()
    root1.quit()
    print 'lol'
    return None

error_msg("This is an error")
    print 'Help'

Before this I have already a Tk() window open so I am using a Toplevel() window.

On running I am getting a window which opens and shows the message. I click on ok and everything just halts. 'lol' does not print in the shell and function never ends (as return statement is not reached), hence 'Help' is also not printed

Any Idea why is this happening ??

Thanks,

pberkes
  • 5,141
  • 1
  • 24
  • 22
infiNity9819
  • 536
  • 2
  • 7
  • 19
  • `destroy` does not exit the mainloop, it only tears down the top window. To exit the loop you need to use `root1.quit()` as well as `root1.destroy()` on clicking the button. – NDevox Mar 31 '15 at 08:09

1 Answers1

3

For a dialog window, instead of creating a new mainloop, you should use wait_window(). This waits until the Toplevel window is closed and then continues execution of the following lines.

So you should replace

root1.mainloop()
root1.quit()

with

root1.wait_window()

For more tips on creating a dialog window see this article on effbot.org.

fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
  • Also I would like to know how to add more than 1 function to the command button I cant find any site on the net. – infiNity9819 Mar 31 '15 at 08:39
  • You can't. If you want multiple functions to run when you click the button you must define a new function which calls all the functions you want to run and set the command to that function. Also see [this question](http://stackoverflow.com/q/11401564/3714930) and [this question](http://stackoverflow.com/q/13865009/3714930). – fhdrsdg Mar 31 '15 at 08:42