The design of the UI, the record of the control ID, is a tedious task.
In addition, the assignment and reading data are cumbersome and very pythonic.
Is there a god-horse approach to elegance? Life was short.
Given that the control has a Name property, it is known through DIR (entry_obj) that it is stored in a _name attribute. The following code is then available:
Entry (frame,name= ' your_id1 '). Grid (Row=x1,column=y1) Entry (frame,name= ' Your_id2 '). Grid (Row=x2,column=y2) ... Entry (frame, name= ' your_idn '). Grid (Row=xn,column=yn)
There are, of course, other input controls, here slightly.
All of its above controls can be obtained by frame.grid_slaves ().
The specific type can then be judged by __class__. On this basis, you can determine which controls are input controls, as follows:
For CTRL in Frame.grid_slaves (): if ctrl.__class__.__name__ in (' Entry ', ' Text ',....): return Truereturn False
In addition, we need a mapping table to correlate the interface controls with our model.
mapper={' model_attr1 ': ctrol_id1, ' model_attr2 ': Ctrol_id2,...., ' model_attrn ': ctrol_idn}
With this mapping table, we can easily set up and read the form data, assuming all the input controls are entry, and then have the code to read the form:
def get_form_data (self): Vals = {}.fromkeys (Self.mapper.keys (), False) ctrls = Dict ([(X._NAME,X) for x in Self.input_ctrls]) for k,v in Self.mapper.items (): ctrl = Ctrls.get (V,FALSE) If CTRL + Ctrl.get (): Vals.update ({k:ctrl.get ()}) #print vals logging.debug (' product form data:%s '%vals) return Vals
As for the description of Input_ctrls, because the dependency of the control, it should essentially be a tree.
A design can be used:
Put the control into the frame, and all of the frames are placed in a list of frame_list.
The controls in a frame are laid out in grid mode. The following code reads all the input controls on the interface:
def _is_input (self, Ctrl_obj): "" "is the input control :p Aram Ctrl_obj: : Return:" "" if Ctrl_obj: Name = Ctrl_obj._name matches= [' txt_ ', ' cb_ '] if (name.split ('_') [0]+ ' _ ') in matches: return True return False def _get_input_ctrls (self, container): if container: cs = container.grid_slaves () Cs.extend (Container.pack_slaves ()) for C in CS: if Self._is_input (c): Self.input_ctrls.append (c) Else: self._get_input_ctrls (c) def get_input_ctrls (self): self.input_ctrls=[] for F in Self.frames: self._get_input_ctrls (f) return Self.input_ctrls
Whether the input controls are judged by the _name attribute. Coding specification constraints are required here.
You can actually rewrite the _is_input logic to make enumeration judgments based on __class__.
The corresponding setting of the value logic for the form is as follows:
def set_data (self, data): if isn't isinstance (data,dict): raise TypeError if not self.mapper: raise "set _mapper method must is called before this method being called. " Ctrls = Dict ([(X._NAME,X) for x in Self.get_input_ctrls ()]))- k,v in Self.mapper.items (): if Data.get (k,false) : ctrl_obj = Ctrls.get (v) if ctrl_obj: ctrl_obj.configure (state= ' normal ') Ctrl_obj.insert (0, Data.get (k)) ctrl_obj.configure (state= ' readonly ')
Tkinter interface Design pythonic design of Python GUI