Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 1 | from Tkinter import * |
| 2 | |
| 3 | # This is a demo program that shows how to |
| 4 | # create radio buttons and how to get other widgets to |
| 5 | # share the information in a radio button. |
| 6 | # |
| 7 | # There are other ways of doing this too, but |
| 8 | # the "variable" option of radiobuttons seems to be the easiest. |
| 9 | # |
| 10 | # note how each button has a value it sets the variable to as it gets hit. |
| 11 | |
| 12 | |
| 13 | class Test(Frame): |
| 14 | def printit(self): |
| 15 | print "hi" |
| 16 | |
| 17 | def createWidgets(self): |
| 18 | |
| 19 | self.flavor = StringVar() |
| 20 | self.flavor.set("chocolate") |
| 21 | |
| 22 | self.radioframe = Frame(self) |
| 23 | self.radioframe.pack() |
| 24 | |
| 25 | # 'text' is the label |
| 26 | # 'variable' is the name of the variable that all these radio buttons share |
| 27 | # 'value' is the value this variable takes on when the radio button is selected |
| 28 | # 'anchor' makes the text appear left justified (default is centered. ick) |
| 29 | self.radioframe.choc = Radiobutton (self.radioframe, {"text" : "Chocolate Flavor", |
| 30 | "variable" : self.flavor, |
| 31 | "value" : "chocolate", |
| 32 | "anchor" : "w", |
| 33 | Pack : {"side" : "top", "fill" : "x"}}) |
| 34 | |
| 35 | self.radioframe.straw = Radiobutton (self.radioframe, {"text" : "Strawberry Flavor", |
| 36 | "variable" : self.flavor, |
| 37 | "anchor" : "w", |
| 38 | "value" : "strawberry", |
| 39 | Pack : {"side" : "top", "fill" : "x"}}) |
| 40 | |
| 41 | self.radioframe.lemon = Radiobutton (self.radioframe, {"text" : "Lemon Flavor", |
| 42 | "anchor" : "w", |
| 43 | "variable" : self.flavor, |
| 44 | "value" : "lemon", |
| 45 | Pack : {"side" : "top", "fill" : "x"}}) |
| 46 | |
| 47 | |
| 48 | # this is a text entry that lets you type in the name of a flavor too. |
| 49 | self.entry = Entry(self, {"textvariable" : self.flavor, |
| 50 | Pack : {"side" : "top", "fill" : "x"}}) |
| 51 | self.QUIT = Button(self, {'text': 'QUIT', |
| 52 | 'fg': 'red', |
| 53 | 'command': self.quit}) |
| 54 | |
| 55 | self.QUIT.pack({'side': 'bottom', 'fill': 'both'}) |
| 56 | |
| 57 | |
| 58 | |
| 59 | def __init__(self, master=None): |
| 60 | Frame.__init__(self, master) |
| 61 | Pack.config(self) |
| 62 | self.createWidgets() |
| 63 | |
| 64 | test = Test() |
| 65 | |
| 66 | test.mainloop() |