return [var.get() for var in self.vars]
class Radiobar(Frame):
def init(self, parent=None, picks=[], side=LEFT, anchor=W):
Frame.init(self, parent)
self.var = StringVar()
self.var.set(picks[0])
for pick in picks:
rad = Radiobutton(self, text=pick, value=pick, variable=self.var)
rad.pack(side=side, anchor=anchor, expand=YES)
def state(self):
return self.var.get()
if name == 'main':
root = Tk()
lng = Checkbar(root, ['Python', 'C#', 'Java', 'C++'])
gui = Radiobar(root, ['win', 'x11', 'mac'], side=TOP, anchor=NW)
tgl = Checkbar(root, ['All'])
gui.pack(side=LEFT, fill=Y)
lng.pack(side=TOP, fill=X)
tgl.pack(side=LEFT)
lng.config(relief=GROOVE, bd=2)
gui.config(relief=RIDGE, bd=2)
def allstates():
print(gui.state(), lng.state(), tgl.state())
from quitter import Quitter
Quitter(root).pack(side=RIGHT)
Button(root, text='Peek', command=allstates).pack(side=RIGHT)
root.mainloop()
To reuse these classes in your scripts, import and call them with a list of the options
that you want to appear in a bar of check buttons or radio buttons. This module’s self-
test code at the bottom of the file gives further usage details. It generates Figure 8-36—
a top-level window that embeds two Checkbars, one Radiobar, a Quitter button to exit,
and a Peek button to show bar states—when this file is run as a program instead of
being imported.
Figure 8-36. buttonbars self-test window
Running GUI Code Three Ways | 483