Advertisement
here2share

# Tk_radiobuttons_demo.py

May 29th, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. # Tkinter_radiobuttons_demo.py
  2.  
  3. from Tkinter import *
  4.  
  5. class RBDemo:
  6.     def __init__(self, win):
  7.         self.v = IntVar()
  8.  
  9.         #Put the first group of radio buttons in their own frame.
  10.         f1 = Frame(win, borderwidth=3, relief=RAISED)
  11.         rb1 = Radiobutton(f1, text="One", variable=self.v, value=1)
  12.         rb2 = Radiobutton(f1, text="Two", variable=self.v, value=2)
  13.         rb3 = Radiobutton(f1, text="Three", variable=self.v, value=3)
  14.         rb1.pack(anchor=W);  rb2.pack(anchor=W);   rb3.pack(anchor=W)
  15.         f1.pack(side=LEFT)
  16.  
  17.         #Button one will be selected by default
  18.         self.v.set(1)
  19.  
  20.  
  21.         #Make a second group of radiobuttons in their own frame.
  22.         #The "g" value is default. (Button with "Green" label)
  23.         self.v2 = StringVar()
  24.         f2 = Frame(win, borderwidth=2, relief=SOLID)
  25.  
  26.         rb4 = Radiobutton(f2, text="Red", variable=self.v2, value="r")
  27.         rb5 = Radiobutton(f2, text="Green", variable=self.v2, value="g")
  28.         rb6 = Radiobutton(f2, text="Blue", variable=self.v2, value="b")
  29.         rb4.pack(anchor=W);  rb5.pack(anchor=W);  rb6.pack(anchor=W);
  30.         f2.pack(side=RIGHT)
  31.         self.v2.set("g")
  32.  
  33.         #Make a button that prints what each value is when clicked
  34.         b = Button(win, text="Try This Out!", command=self.clicked)
  35.         b.pack(side=BOTTOM, fill=BOTH, expand=1)
  36.  
  37.  
  38.     def clicked(self):
  39.         print "Button Clicked!"
  40.         print "v is:", self.v.get()
  41.         print "v2 is:", self.v2.get()
  42.         print
  43.  
  44.  
  45.  
  46. root = Tk()
  47. app = RBDemo(root)
  48. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement