Python Forum
[Tkinter] Close Toplevel after clicking button - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Close Toplevel after clicking button (/thread-11486.html)



Close Toplevel after clicking button - jollydragon - Jul-11-2018

After runing below code, got below error.
How to colse the Toplevel window after clicking the button in my Python v3.6.5?
Thanks for any help or suggestion.

Error:
_tkinter.TclError: bad window path name ".!toplevel"
def mdfNames(mdf):
     mdf.destroy()
     

def mdfPanel():
     mdf = tkinter.Toplevel()
     mdfSize = 220
     mdf.geometry('%dx%d+%d+%d' % (mdfSize, mdfSize, (mdf.winfo_screenwidth()-mdfSize)/2,
                    (mdf.winfo_screenheight()-mdfSize)/2))                

     tkinter.Button(mdf, text = "Start", command=mdfNames(mdf), justify = tkinter.CENTER).grid(row=7,column =1, columnspan =2)



RE: Close Toplevel after clicking button - Larz60+ - Jul-11-2018

Please use error tags, and show entire error traceback, always as it contains valuable information.
I added error tags above.


RE: Close Toplevel after clicking button - wuf - Jul-11-2018

Hi jollydragon

from functools import partial
import tkinter

APP_TITLE = "Main Window"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 350
APP_HEIGHT = 200


def mdfNames(mdf):
    mdf.destroy()
      
def mdfPanel():
    mdf = tkinter.Toplevel()
    mdf.title("Top Level Window")
    mdfSize = 220
    mdf.geometry('%dx%d+%d+%d' % (
        mdfSize, mdfSize, (mdf.winfo_screenwidth()-mdfSize)/2,
        (mdf.winfo_screenheight()-mdfSize)/2))                
 
    tkinter.Button(mdf, text="Start", command=partial(mdfNames, mdf)
        ).grid(row=7,column=1, columnspan=2)
           
def main():
    app_win = tkinter.Tk()
    app_win.title(APP_TITLE)
    app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    
    app = mdfPanel()
    
    app_win.mainloop()
 
 
if __name__ == '__main__':
    main()      
Replace the following:
command=mdfNames(mdf)
By:
command=partial(mdfNames, mdf)
Instead of partial you can of course also use the lambda function.

wuf ;-)


RE: Close Toplevel after clicking button - jollydragon - Jul-12-2018

(Jul-11-2018, 10:19 AM)wuf Wrote: Hi jollydragon

Instead of partial you can of course also use the lambda function.

wuf ;-)

Thank you very much.
You are right it works well too with Lambda. But how should I understand it? How does Lambda work with it? And it doesn't if without Lambda?