Reputation: 1678
I'm trying to create a popup with one Entry
and one Button
. I need to get whatever the user input into that entry and bring back to my main window. I'm using Python 3.6.0. I have the following code (which was inspired by the code from this question):
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.server_address = None
self.master = master
self.master.title('Application')
self.master.resizable(width=False, height=False)
self.master.minsize(width=800, height=600)
self.master.maxsize(width=800, height=600)
self.grid()
self.init_menubar()
def init_menubar(self):
menubar = Menu(self.master)
self.master.config(menu=menubar)
filemenu = Menu(menubar, tearoff=False)
filemenu.add_command(label="Set remote server", command=self.call_server_config)
filemenu.add_command(label="Set refresh delay", command=None)
filemenu.add_separator()
filemenu.add_command(label="Close", command=self.master.destroy)
menubar.add_cascade(label='File', menu=filemenu)
helpmenu = Menu(menubar, tearoff=False)
helpmenu.add_command(label="Help", command=None)
menubar.add_cascade(label='Help', menu=helpmenu)
def call_server_config(self):
popup = ServerConfig(self.master)
self.master.wait_window(popup.top)
print('DEBUG:', popup.value)
self.create_widgets()
class ServerConfig(Frame):
def __init__(self, master=None):
super().__init__(master)
self.top = Toplevel(master)
self.label = Label(self.top, text="Server address")
self.label.pack()
self.entry = Entry(self.top)
self.entry.pack()
self.button = Button(self.top, text='Ok', command=self.top.destroy())
self.value = self.entry.get()
if __name__ == '__main__':
root = Tk()
app = Application(master=root)
app.mainloop()
Which gives me the error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\<...>\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "D:/Documents/<...>/tk-client.py", line 45, in call_server_config
popup = ServerConfig(self)
File "D:/Documents/<...>/tk-client.py", line 60, in __init__
self.button = Button(self.top, text='Ok', command=self.top.destroy())
File "C:\Users\<...>\Python\Python36\lib\tkinter\__init__.py", line 2363, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Users\<...>\Python\Python36\lib\tkinter\__init__.py", line 2293, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: bad window path name ".!application.!toplevel"
Upvotes: 2
Views: 1401
Reputation: 33714
This piece of code caused the exception:
self.button = Button(self.top, text='Ok', command=self.top.destroy())
The reason for this is because self.top.destroy()
was first evaluated when you created the button. Therefore you couldn’t place the button on self.top
as it was destroyed. You will need to change it to this:
self.button = Button(self.top, text='Ok', command=self.top.destroy)
command=self.top.destroy
which only execute self.top.destroy
when the button get pressed
Upvotes: 2