exit from Tkinter mainloop Python 2.7
Christian Gollwitzer
auriocus at gmx.de
Tue Feb 23 17:29:31 EST 2016
Am 23.02.16 um 22:39 schrieb kevind0718 at gmail.com:
> from Tkinter import *
>
> def butContinue():
> root1.destroy()
> [...]
> entryName = Entry(root1).grid(row=1, column=1, pady=5)
> [...]
> butGo = Button(root1, text=" Continue " , command=butContinue ).grid(row=3, column=1, sticky=W, pady=10)
>
> root1.mainloop()
>
> print entryName.get("1.0", "end-1c" )
> When I click on butGo I get the error below.
> So is this some sort of scope error?
> why does entryName not exist for me to grab it's value?
You call destroy() on the root window of Tk. If you destroy a window,
that will destroy all of it's children. Therefore by deleting the root
window, also the entryName widget was deleted. You need to export the
values before you close the window, i.e. in the butContinue() function.
There is another pitfall with Tk: if you delete the main window, you can
get problems if you try to recreate it. If you want to show several
pages in sequence or the like, you should withdraw the main window
root1.withdraw()
and popup a fresh toplevel by
t=Toplevel()
and do everything in t. Once you are finished, destroying t will keep
your application alive (because the real root window is just hidden)
Another comment:
> root1.geometry("500x250")
Do not do this. The grid and pack geometry managers compute the needed
space automatically. A fixed size in pixels can easily break, when you
go to another computer with a different font size, screen resolution, or
a different OS. If you are not satisfied with the whitespace, use the
padding options of grid or pack.
Christian
More information about the Python-list
mailing list