/Demo/tkinter/matt/radiobutton-simple.py

http://unladen-swallow.googlecode.com/ · Python · 62 lines · 35 code · 14 blank · 13 comment · 0 complexity · b5a02331108f65b3db553a994391a2a2 MD5 · raw file

  1. from Tkinter import *
  2. # This is a demo program that shows how to
  3. # create radio buttons and how to get other widgets to
  4. # share the information in a radio button.
  5. #
  6. # There are other ways of doing this too, but
  7. # the "variable" option of radiobuttons seems to be the easiest.
  8. #
  9. # note how each button has a value it sets the variable to as it gets hit.
  10. class Test(Frame):
  11. def printit(self):
  12. print "hi"
  13. def createWidgets(self):
  14. self.flavor = StringVar()
  15. self.flavor.set("chocolate")
  16. self.radioframe = Frame(self)
  17. self.radioframe.pack()
  18. # 'text' is the label
  19. # 'variable' is the name of the variable that all these radio buttons share
  20. # 'value' is the value this variable takes on when the radio button is selected
  21. # 'anchor' makes the text appear left justified (default is centered. ick)
  22. self.radioframe.choc = Radiobutton(
  23. self.radioframe, text="Chocolate Flavor",
  24. variable=self.flavor, value="chocolate",
  25. anchor=W)
  26. self.radioframe.choc.pack(fill=X)
  27. self.radioframe.straw = Radiobutton(
  28. self.radioframe, text="Strawberry Flavor",
  29. variable=self.flavor, value="strawberry",
  30. anchor=W)
  31. self.radioframe.straw.pack(fill=X)
  32. self.radioframe.lemon = Radiobutton(
  33. self.radioframe, text="Lemon Flavor",
  34. variable=self.flavor, value="lemon",
  35. anchor=W)
  36. self.radioframe.lemon.pack(fill=X)
  37. # this is a text entry that lets you type in the name of a flavor too.
  38. self.entry = Entry(self, textvariable=self.flavor)
  39. self.entry.pack(fill=X)
  40. self.QUIT = Button(self, text='QUIT', foreground='red',
  41. command=self.quit)
  42. self.QUIT.pack(side=BOTTOM, fill=BOTH)
  43. def __init__(self, master=None):
  44. Frame.__init__(self, master)
  45. Pack.config(self)
  46. self.createWidgets()
  47. test = Test()
  48. test.mainloop()