So how do you use models in tkinter

It's important to EXPLICITLY each button a VALUE.

Radiobutton(app, text = "First Class", value = "First Class").pack() Radiobutton(app, text = "Next Business Day", value = "Next Business Day").pack()

It's important to EXPLICITLY each button a VALUE.

Radiobutton(app, text = "First Class", value = "First Class").pack() Radiobutton(app, text = "Next Business Day", value = "Next Business Day").pack()

You then need to create a model for the radio buttons to share. In tkinter, models are called control variables, and control variables that store text

Radiobutton(app, text = "First Class", value = "First Class", variable = service).pack()

Radiobutton(app, text = "Next Business Day", value = "Next Business Day", variable = service).pack()

This code will now give us a pair of buttons that work together. If you select one, the other will automatically become deselected:

are called StringVars:

g This sets the StringVar to the special value service.set(None^^— "None" which means "No value."

service = StringVar()

g This sets the StringVar to the special value service.set(None^^— "None" which means "No value."

Click the SECOND option and

-Q First Class

Click the SECOND option and

And if you ever need to read or change the model value in the code, you just need to call the StringVar's get ( ) or set ( ) methods:

>>> print(service.get()) "Next Business Day"

ThiS returns the Current

ThiS returns the Current

O First Class *Q Next Business Day

Poo] Puzzje

This is the section of the program that creates the depot radio buttons. See if you can complete it using the fragments of code from the pool. Be warned: you might not need all of the pieces...

depot.set(None)

Note: each thing from the pool can be used only once!

depot= value=

depot= value=

What piece of code would you use to make sure all of the radio buttons are cleared after the record is saved?

depot buttons

Poo] Puzzje Solution

This is the section of the program that creates the depot radio buttons. You were asked to see if you could complete it using the fragments of code from the pool. Not all of the pieces were needed.

Label(app, text = "Depot:").pack() depot = StringVar()

depot.set(None)

Label(app, text = "Depot:").pack() depot = StringVar()

depot.set(None)

Radi obut t on(app,

variable =

depot,

text =

"Cambridge, MA" , value

= "Cambridge,

MA").pack()

Radi obut t on(app,

variable =

depot,

text =

"Cambridge, UK" , value

= "Cambridge,

UK").pack()

Radi obut t on(app,

variable =

depot,

text =

"Seattle, WA", value =

"Seattle, WA"

.pack()

Note: each thing from

Note: each thing from

r ^harpen your pencil Solution of the m be selected.

What piece of code would you use to make sure all of the radio buttons are cleared after the record is saved?

Became n°ne f the radio ■ depot-set(None) buttons have this value, n°ne

Was this article helpful?

0 0

Post a comment