tkinter, radiobuttons, variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ichor
    New Member
    • Sep 2006
    • 3

    #1

    tkinter, radiobuttons, variables

    This is my code. I've tried a trillion different things. But basically what I want is the radiobuttons to appear. Select one and hit the okay button. Then the screen prints out the var of which button was selected (just to verify). And I want the whole program to wait and do nothing until this process is done. (What I'd want to do next is another radiobutton set, but I'm not there yet.)

    Help! I've been researching this online all day today. I've found tons of posts with people with the same problems, but either the solutions weren't available, or made no sense to me.

    Thank you very much in advance.





    from Tkinter import *

    master = Tk()

    var = IntVar()

    Radiobutton(mas ter, text="One", variable=var, value=1).pack(a nchor=W)
    Radiobutton(mas ter, text="Two", variable=var, value=2).pack(a nchor=W)

    Button(master, text='OK!',comm and=master.dest roy).pack()

    print "moo1 var = ",var
    master.wait_var iable
    print "moo2 var = ",var

    master.mainloop ()
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    I future, read posting or reply guidelines to learn how to use CODE tags so you post looks like this:

    [code=python]
    from Tkinter import *

    master = Tk()

    var = IntVar()
    var.set(1) # Set the defalt value to something

    # I like to keep the instanciated button around in case I want to config it later
    rb1 = Radiobutton(mas ter, text="One", variable=var, value=1)
    rb2 = Radiobutton(mas ter, text="Two", variable=var, value=2)
    rb1.pack(anchor =W)
    rb2.pack(anchor =W)

    #This should be what you needed.
    def print_rb_state( ):
    print "var = ", var.get()

    Button(master, text='Print State!',command =print_rb_state ).pack()


    master.mainloop ()
    [/code]

    Comment

    Working...