0
app=Tk()
age=IntVar()
name=StringVar()
id=IntVar()
def add_user():
    app1=Tk()
    L1 = Message(app1,text="Name")
    L1.pack( side = LEFT)
    E1 = Entry(app1,textvariable=name)
    E1.pack()
    L2 = Message(app1,text="\nAge")
    L2.pack( side = LEFT)
    E2 = Spinbox(app1,from_=1,to_=100,textvariable=age)
    E2.pack()
    l3=Message(app1,text="\nId")
    l3.pack()
    e3=Spinbox(app1,from_=1,to_=100,textvariable=id)
    e3.pack()
    b5=Button(app1,text="submit",command=app1.destroy)
    b5.pack()
    app1.mainloop()
    print age.get(),name.get(),id.get()
    return

b1=Button(app,command=add_user,relief=RIDGE,text="add patient details")
b1.pack(side=BOTTOM)
app.mainloop()

the print statement doesn't print the correct values,it always prints the default values.I don't understand where I made a mistake

Bharath
  • 171
  • 1
  • 15
  • Works fine when you make the "dialog" top-level. Probably a problem with how you start two Tk instances. Also note how the output is only printed after the _main_ window is closed. – tobias_k Sep 10 '14 at 09:31
  • yeah thanks just realized that I need to print after closing the main window – Bharath Sep 10 '14 at 09:38
  • Still strange... you should investigate how to properly open a modal dialogue in Tkinter (not sure how right now, either). – tobias_k Sep 10 '14 at 09:39

1 Answers1

0

The reason you can't get the values is that the widgets have been destroyed once mainloop has exited.

The bigger problem in your code is that you are creating two instances of Tk. Tkinter isn't designed to work that way. A proper Tkinter program creates exactly one instance of Tk, and exits when that one instance is destroyed. If you want to create more than one window, the second and subsequent windows need to be instances of Toplevel.

You might find the answer to this question useful: Correct way to implement a custom popup tkinter dialog box

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685