Tkinter --> Why multiple windows

kevind0718 at gmail.com kevind0718 at gmail.com
Fri Mar 25 09:41:48 EDT 2016


On Thursday, March 24, 2016 at 9:24:44 PM UTC-4, Wildman wrote:
> On Thu, 24 Mar 2016 13:24:16 -0700, kevind0718 wrote:
> 
> > Hello:
> > 
> > newbie Tkinter question
> > 
> > If I run the code below two windows appear.
> > One empty and one with the text box and button.
> > 
> > Why?  please
> > 
> > KD
> > 
> > 
> > 
> > from Tkinter import *
> > 
> > class MyDialog:
> >     def __init__(self, parent):
> > 
> >         top = self.top = Toplevel(parent)
> > 
> >         Label(top, text="Value").pack()
> > 
> >         self.e = Entry(top)
> >         self.e.pack(padx=5)
> > 
> >         b = Button(top, text="OK", command=self.ok)
> >         b.pack(pady=5)
> > 
> >     def ok(self):
> > 
> >         print "value is", self.e.get()
> > 
> >         self.top.destroy()
> > 
> > 
> > root = Tk()
> > 
> > d = MyDialog(root)
> > 
> > root.wait_window(d.top)
> 
> Try this:
> 
> from Tkinter import *
> 
> class MyDialog(Frame):
> 
>     def __init__(self, parent):
>         Frame.__init__(self, parent)
>         self.parent = parent
>         self.pack(fill=BOTH, expand=1)
>         self.parent.title("MyDialog")
>         Label(self, text="Value").pack()
>         self.e = Entry(self)
>         self.e.pack(padx=5)
>         self.b = Button(self, text="OK", command=self.ok)
>         self.b.pack(pady=5)
> 
>     def ok(self):
>         print "value is", self.e.get()
> 
> root = Tk()
> d = MyDialog(root)
> root.mainloop()
> 
> -- 
> <Wildman> GNU/Linux user #557453
> May the Source be with you.


Looking at the lines below 
As related to the code I posted above.
I believe this code will allow me to instantiate the class
MyDialog, wait until the window is destroyed and then 
continue on my merry way.

root = Tk()
d = MyDialog(root)
root.wait_window(d.top) 


Now if I pass an instance of  Unamepword into the constructor of
MyDialog(unamepword) , I can modify uStr and pWord in MyDialog and 
the local copy will get updated.  
Correct?

class Unamepword:
    ## 
    ## class to hold user name and pWord for Database
    uName = None
    pWord = None
    def __init__(self, uStr, pStr):
        self.uName = uStr
        self.pWord = pStr

Many thanks for your attention to this matter.

KD



More information about the Python-list mailing list