Python Forum

Full Version: Close Toplevel after clicking button
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
Please use error tags, and show entire error traceback, always as it contains valuable information.
I added error tags above.
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 ;-)
(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?