Skip to content

Commit a143cdc

Browse files
committed
update application per ch5 revisions
1 parent e5c7909 commit a143cdc

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

Chapter05/data_entry_app.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,32 @@ def _focusout_validate(self, **kwargs):
283283

284284
return valid
285285

286-
class ValidatedRadio(ttk.Radiobutton):
287-
"""A validated radio button"""
288286

289-
def __init__(self, *args, error_var=None, **kwargs):
287+
class ValidatedRadioGroup(ttk.Frame):
288+
"""A validated radio button group"""
289+
290+
def __init__(
291+
self, *args, variable=None, error_var=None,
292+
values=None, button_args=None, **kwargs
293+
):
290294
super().__init__(*args, **kwargs)
295+
self.variable = variable or tk.StringVar()
291296
self.error = error_var or tk.StringVar()
292-
self.variable = kwargs.get("variable")
293-
self.bind('<FocusOut>', self._focusout_validate)
297+
self.values = values or list()
298+
button_args = button_args or dict()
294299

295-
def _focusout_validate(self, *_):
300+
for v in self.values:
301+
button = ttk.Radiobutton(
302+
self, value=v, text=v, variable=self.variable, **button_args
303+
)
304+
button.pack(side=tk.LEFT, ipadx=10, ipady=2, expand=True, fill='x')
305+
self.bind('<FocusOut>', self.trigger_focusout_validation)
306+
307+
def trigger_focusout_validation(self, *_):
296308
self.error.set('')
297309
if not self.variable.get():
298310
self.error.set('A value is required')
299311

300-
def trigger_focusout_validation(self):
301-
self._focusout_validate()
302312

303313
class BoundText(tk.Text):
304314
"""A Text widget with a bound variable."""
@@ -353,23 +363,22 @@ def __init__(
353363

354364
# setup the variable
355365
if input_class in (
356-
ttk.Checkbutton, ttk.Button, ttk.Radiobutton, ValidatedRadio
366+
ttk.Checkbutton, ttk.Button,
367+
ttk.Radiobutton, ValidatedRadioGroup
357368
):
358369
input_args["variable"] = self.variable
359370
else:
360371
input_args["textvariable"] = self.variable
361372

362373
# Setup the input
363-
if input_class in (ttk.Radiobutton, ValidatedRadio):
374+
if input_class == ttk.Radiobutton:
364375
# for Radiobutton, create one input per value
365376
self.input = tk.Frame(self)
366377
for v in input_args.pop('values', []):
367378
button = input_class(
368379
self.input, value=v, text=v, **input_args
369380
)
370381
button.pack(side=tk.LEFT, ipadx=10, ipady=2, expand=True, fill='x')
371-
self.input.error = getattr(button, 'error')
372-
self.input.trigger_focusout_validation = button.trigger_focusout_validation
373382
else:
374383
self.input = input_class(self, **input_args)
375384
self.input.grid(row=1, column=0, sticky=(tk.W + tk.E))
@@ -460,7 +469,7 @@ def __init__(self, *args, **kwargs):
460469

461470
# line 2
462471
LabelInput(
463-
r_info, "Lab", input_class=ValidatedRadio,
472+
r_info, "Lab", input_class=ValidatedRadioGroup,
464473
var=self._vars['Lab'], input_args={"values": ["A", "B", "C"]}
465474
).grid(row=1, column=0)
466475
LabelInput(

0 commit comments

Comments
 (0)