Tkinter module test: widget class not inserted in application frame

Rich Shepard rshepard at appl-ecosys.com
Fri Jun 17 13:06:12 EDT 2022


On Fri, 17 Jun 2022, MRAB wrote:

> You haven't shown the code for common_classes.LabelInput, but I'm guessing 
> that the first argument should be the parent.

Here's the LabelInput class:
class LabelInput(tk.Frame):
     """ A widget containing a label and input together. """

     def __init__(self, parent, label='', input_class=ttk.Entry,
                  input_var=None, input_args=None, label_args=None,
                  **kwargs):
         super().__init__(parent, **kwargs)
         label_args = label_args or {}
         input_args = input_args or {}
         self.variable = input_var,

         if input_class in (ttk.Checkbutton, ttk.Button, ttk.Radiobutton):
             input_args["text"] = label
             input_args["variable"] = input_var
         else:
             self.label = ttk.Label(self, text=label, **label_args)
             self.label.grid(row=0, column=0, sticky=(tk.W + tk.E))
             input_args["textvariable"] = input_var

         self.input = input_class(self, **input_args)
         self.input.grid(row=1, column=0, sticky=(tk.W + tk.E))
         self.columnconfigure(0, weight=1)
         self.error = getattr(self.input, 'error', tk.StringVar())
         self.error_label = ttk.Label(self, textvariable=self.error)
         self.error_label.grid(row=2, column=0, sticky=(tk.W + tk.E))

     def grid(self, sticky=(tk.E + tk.W), **kwargs):
         super().grid(sticky=sticky, **kwargs)

     def get(self):
         try:
             if self.variable:
                 return self.variable.get()
             elif type(self.input) == tk.Text:
                 return self.input.get('1.0', tk.END)
             else:
                 return self.input.get()
         except (TypeError, tk.TclError):
             # happens when numeric fields are empty.
             return ''

     def set(self, value, *args, **kwargs):
         if type(self.variable) == tk.BooleanVar:
                 self.variable.set(bool(value))
         elif self.variable:
                 self.variable.set(value, *args, **kwargs)
         elif type(self.input) in (ttk.Checkbutton, ttk.Radiobutton):
             if value:
                 self.input.select()
             else:
                 self.input.deselect()
         elif type(self.input) == tk.Text:
             self.input.delete('1.0', tk.END)
             self.input.insert('1.0', value)
         else:
             self.input.delete(0, tk.END)
             self.input.insert(0, value)

> You're passing in the _class_ ConactNameInput, but I'm guessing that it 
> should be an _instance_ of that class, in this case, 'self'.

Haven't I done this in the application class?
class NameApplication(tk.Tk):
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.title("Contact Name")
         self.geometry("800x600")
         self.resizable(width = False, height = False)
         ContactNameInput(self).grid(row = 0, column = 0, sticky=('EWNS'))
         self.columnconfigure(0, weight=1)

If not, where do I specify the instance of the ContactNameInput class?

Thanks,

Rich


-- 
Richard Shepard, Ph.D.			The Environmental Issues Doctor
Applied Ecosystem Services, LLC
Troutdale, OR 97060 USA    503-667-4517     www[dot]appl-ecosys[dot]com


More information about the Python-list mailing list