How Do You Make A Custom Popup In Tkinter Using Two Seperate Classes And Inheritance?
Solution 1:
Okay. I believe that I've implemented the stuff you are having problems with in your code, see below for further explanations:
from Tkinter import *
classHome(Frame):
def__init__(self, master):
Frame.__init__(self, master)
self.entryvalue = None# this will store the popop entry value
self.call_popup(self)
print("Popup closed!")
print("Entry value:" + self.entryvalue)
defcall_popup(self, master):
options_popup = Options(master)
options_popup.wait_window()
classOptions(Toplevel):
def__init__(self, master):
Toplevel.__init__(self, master)
self.master = master
# add an entry widget
self.e1 = Entry(self)
self.e1.pack()
# add a button
b1 = Button(self, text="Popup button", command=self.buttonpressed)
b1.pack()
defbuttonpressed(self):
self.master.entryvalue = self.e1.get()
self.exit_popup()
defexit_popup(self):
self.destroy()
root = Tk()
Home(root)
root.mainloop()
For your first question. You need to provide the popup/options object with a reference to the object where you want your data to end up, in this case in the home object. As you can see I'm passing this as a new argument to the init method of the popup. I've then added a command to the button which will then set the entryvalue variable in the home object. This could also have been a function call or similiar.
For having the code wait for the window to finish, you should call the wait_window() method on the options_popup object, as this is the one you are waiting for. You could also move it to the init method of the popup window and invoke it on self instead.
For more information regarding dialog windows, you should look at this article. This dialog support class example is pretty good I think.
Hope it helps!
Post a Comment for "How Do You Make A Custom Popup In Tkinter Using Two Seperate Classes And Inheritance?"