@@ -274,23 +274,31 @@ def _focusout_validate(self, **kwargs):
274274
275275 return valid
276276
277- class ValidatedRadio (ttk .Radiobutton ):
278- """A validated radio button"""
277+ class ValidatedRadioGroup (ttk .Frame ):
278+ """A validated radio button group """
279279
280- def __init__ (self , * args , error_var = None , ** kwargs ):
280+ def __init__ (
281+ self , * args , variable = None , error_var = None ,
282+ values = None , button_args = None , ** kwargs
283+ ):
281284 super ().__init__ (* args , ** kwargs )
285+ self .variable = variable or tk .StringVar ()
282286 self .error = error_var or tk .StringVar ()
283- self .variable = kwargs .get ("variable" )
284- self .bind ('<FocusOut>' , self ._focusout_validate )
287+ self .values = values or list ()
288+ button_args = button_args or dict ()
289+
290+ for v in self .values :
291+ button = ttk .Radiobutton (
292+ self , value = v , text = v , variable = self .variable , ** button_args
293+ )
294+ button .pack (side = tk .LEFT , ipadx = 10 , ipady = 2 , expand = True , fill = 'x' )
295+ self .bind ('<FocusOut>' , self .trigger_focusout_validation )
285296
286- def _focusout_validate (self , * _ ):
297+ def trigger_focusout_validation (self , * _ ):
287298 self .error .set ('' )
288299 if not self .variable .get ():
289300 self .error .set ('A value is required' )
290301
291- def trigger_focusout_validation (self ):
292- self ._focusout_validate ()
293-
294302
295303class BoundText (tk .Text ):
296304 """A Text widget with a bound variable."""
@@ -328,7 +336,7 @@ class LabelInput(ttk.Frame):
328336 field_types = {
329337 FT .string : RequiredEntry ,
330338 FT .string_list : ValidatedCombobox ,
331- FT .short_string_list : ValidatedRadio ,
339+ FT .short_string_list : ValidatedRadioGroup ,
332340 FT .iso_date_string : DateEntry ,
333341 FT .long_string : BoundText ,
334342 FT .decimal : ValidatedSpinbox ,
@@ -372,24 +380,23 @@ def __init__(
372380
373381 # setup the variable
374382 if input_class in (
375- ttk .Checkbutton , ttk .Button , ttk .Radiobutton , ValidatedRadio
383+ ttk .Checkbutton , ttk .Button , ttk .Radiobutton , ValidatedRadioGroup
376384 ):
377385 input_args ["variable" ] = self .variable
378386 else :
379387 input_args ["textvariable" ] = self .variable
380388
381389 # Setup the input
382- if input_class in ( ttk .Radiobutton , ValidatedRadio ) :
390+ if input_class == ttk .Radiobutton :
383391 # for Radiobutton, create one input per value
384392 self .input = tk .Frame (self )
385393 for v in input_args .pop ('values' , []):
386394 button = input_class (
387395 self .input , value = v , text = v , ** input_args
388396 )
389397 button .pack (side = tk .LEFT , ipadx = 10 , ipady = 2 , expand = True , fill = 'x' )
390- self .input .error = getattr (button , 'error' )
391- self .input .trigger_focusout_validation = \
392- button ._focusout_validate
398+ else :
399+ self .input = input_class (self , ** input_args )
393400 self .input .grid (row = 1 , column = 0 , sticky = (tk .W + tk .E ))
394401 self .columnconfigure (0 , weight = 1 )
395402
0 commit comments