Search code examples
pythonfunctiontkinterwindowtoplevel

How do I make a line of code run after specifically closing a top level window in tkinter


I have this bit of code

def choosesize():
    global textsize

    window2 = Toplevel()

    label = Label(window2,
                  text='Type the size you want for the text')
    label.pack()

    chooses = Text(window2,
                   height=1,
                   width=10)
    chooses.insert(INSERT, str(textsize))
    chooses.pack()

    butt = Button(window2,
                  text='Confirm',
                  command=lambda: chooses.get(1.0, 'end-1c') == textsize)
    butt.pack()

    window2.mainloop()
    print(str(textsize))
    note.configure(font=(font, textsize))

I want the stuff after window2.mainloop() to happen after I close the top level window that opens with this function, but for some reason those lines of code only run when I close the bottom window


Solution

  • Simply change window2.mainloop() to window2.wait_window().

    Note that it is not recommended to call .mainloop() more than once.