Question

Given below is the code for creating independent windows using Tkinter and Python:

import Tkinter

Tkinter.NoDefaultRoot()

win1=Tkinter.Tk()
win2=Tkinter.Tk()

Tkinter.Button(win1, text='Woho!',command=win1.destroy()).pack()
Tkinter.Button(win2, text='Woho!',command=win2.destroy()).pack()

win1.mainloop()

On execution it displays:

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\eclipse\Python Projects\Project 1\Source1\mod.py", line 8, in <module>
    Tkinter.Button(win1, text='Woho!',command=win1.destroy()).pack()
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2106, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2036, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: can't invoke "button" command:  application has been destroyed

I am new to Python and hence so not understand what it means. Where am I going wrong?

Was it helpful?

Solution

Remove () from win1.destroy() and win2.destroy().

Tkinter.Button(win1, text='Woho!',command=win1.destroy()).pack()
Tkinter.Button(win2, text='Woho!',command=win2.destroy()).pack()
                                                      ^^

It cause win1.destroy method call, and use the return value of the method as callback, instead of the method itself.; cause the main window destroy before Button creation.

Tkinter.Button(win1, text='Woho!',command=win1.destroy).pack()
Tkinter.Button(win2, text='Woho!',command=win2.destroy).pack()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top