[Tutor] Broblem with exiting a Tkinter app

Kent Johnson kent37 at tds.net
Sat Dec 4 20:38:06 CET 2004


Mark Kels wrote:
> Hi all ,
> I got 2 questions for you guys.
> 
> The fist question:
> I wrote small Tkinter app while laerning about the Radiobutton widget,
> and I added a "Quit" button, like this:
> bb=Button(root, text="Quit", fg="BLUE", command=root.quit).pack()
> When I pressed the button the app crashed and I got an error message (
> program is not responding ) from windows.
> I tried to add a frame to the program and then exit the frame, like this:
> bb=Button(f, text="Quit", fg="BLUE", command=f.quit).pack()
> But the result is the same...
> 
> Here is the full source code of the app:
> from Tkinter import *
> import sys
> root=Tk()
> f=Frame(root)
> text=Label(root, text="how old are you?").pack()
> v = IntVar(root)
> Radiobutton(f, text="less than 13", variable=v, value=1).pack(side=LEFT)
> Radiobutton(f, text="13-20", variable=v, value=2).pack(side=LEFT)
> Radiobutton(f, text="20-40", variable=v, value=3).pack(side=LEFT)
> Radiobutton(f, text="40+", variable=v, value=4).pack(side=LEFT)
> bb=Button(f, text="Quit", fg="BLUE", command=f.quit).pack()
> f.pack()
> root.mainloop()

This program works fine for me on Win2K. How are you running the program?
> 
> The second question:
> I dont understand how to get the input fron the radio buttons...
> It doesnt returns any value, so how can the app know what the user chose?

Read the value from the variable associated with the buttons - v. For example if you define
def quit():
     print 'You chose button', v.get()
     f.quit()

and change the command on bb to command=quit, the program will print the selection value on exit.
(You have to define quit before bb or you will get a NameError.)

Kent
> 
> Thanks!!
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list