Tkinter --> Why multiple windows

Wildman best_lay at yahoo.com
Thu Mar 24 21:24:33 EDT 2016


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.



More information about the Python-list mailing list