Page 320 -
P. 320
guis and data
The model stays the same
Think for a moment about the model you are using with the radio buttons.
It represents the name of the chosen depot, which you want to keep for the
option menu. If the model stays the same, then your code, which currently
looks like this:
depot = StringVar()
depot.set(None)
Radiobutton(app, variable = depot, text = "Cambridge, MA", value = "Cambridge, MA").pack()
Radiobutton(app, variable = depot, text = "Cambridge, UK", value = "Cambridge, UK").pack()
Radiobutton(app, variable = depot, text = "Seattle, WA", value = "Seattle, WA").pack()
can be replaced with code like this:
depot = StringVar()
depot.set(None)
OptionMenu(app, depot, "Cambridge, MA", "Cambridge, UK", "Seattle, WA").pack()
The second parameter must A list of all the options that
be the model and it doesn't appear in the widget
need the “variable =" bit.
This is the same
model as before.
Cambridge, MA
But wait... you don't have to list all the values like that
It looks like a lot of work to put all of the options in the actual function call to
OptionMenu(), doesn’t it? After all, there’s a large list of depots.
Thankfully, Python comes to the rescue. If you have the options already
stored in a list:
depots = ["Cambridge, MA", "Cambridge, UK", "Seattle, WA"]
This * means “T ake the rest of
you can pass the entire list instead of separate values like this:
the parameters from this list
OptionMenu(app, depot, *depots).pack() and insert them here."
Now let’s put the pieces together.
you are here 4 285