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,

4

1 回答 1

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.

于 2015-03-31T08:05:56.950 回答