Python Forum
[Tkinter] Close Toplevel after clicking button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Close Toplevel after clicking button
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.

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

def mdfPanel():
     mdf = tkinter.Toplevel()
     mdfSize = 220
     mdf.geometry('%dx%d+%d+%d' % (mdfSize, mdfSize, (mdf.winfo_screenwidth()-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

def mdfNames(mdf):
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,
    tkinter.Button(mdf, text="Start", command=partial(mdfNames, mdf)
        ).grid(row=7,column=1, columnspan=2)
def main():
    app_win = tkinter.Tk()
    app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    app = mdfPanel()
if __name__ == '__main__':
Replace the following:
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?

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Images in Toplevel() finndude 4 1,150 Mar-09-2021, 09:39 AM
Last Post: finndude
  Create image on a Toplevel using tkinter ViktorWong 3 3,154 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets iconit 2 839 Apr-28-2020, 06:50 AM
Last Post: iconit
  Need tkinter help with clicking buttons pythonprogrammer 2 850 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 1,149 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 2,213 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ? chano 6 1,843 May-27-2019, 04:24 PM
Last Post: Yoriz
  [Tkinter] Toplevel window and global widgets? KevinBrown 3 2,612 Apr-25-2019, 06:02 PM
Last Post: Yoriz
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 4,265 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] How to get & delete details from each input by clicking a button Vicolas 6 1,481 Feb-01-2019, 11:00 AM
Last Post: Vicolas

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020