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
#1
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)
Reply
#2
Please use error tags, and show entire error traceback, always as it contains valuable information.
I added error tags above.
Reply
#3
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 ;-)
Reply
#4
(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?
Reply


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

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