class StringEntry: """String class for entry rationalised with integer and float classes Parameters ---------- parent : str parent handle lf_text : str text on LabelFrame def_inp : str default text colour : str frame colour mod : bool enable or disable state switch Returns ------- string """ def __init__(self, parent, lf_text, def_inp="", colour='brown', mod=False): self.parent = parent self.lf_text = lf_text # self.mess_text = mess_text self.mod = mod self.ent0 = None # for entry self.cb_opt = None # for check option self.out_var = StringVar() self.out_var.set(def_inp) self.construct(colour) # changed def construct(self, colour): # changed """construct of colour style Parameters ---------- colour : str frame colour Returns ------- None """ self.farbe = farbe = {'blue': 'light blue', 'brown': 'brown1', 'green': 'light green', 'pink': '#EAAFBF'} colour = colour if colour in farbe else 'brown' self.colour = colour st1 = Style() st1.theme_use('default') st1.configure(colour + '.TLabelframe', background='#C9B99B') st1.configure(colour + '.TLabelframe.Label', background=farbe[colour]) st1.configure(colour + '.TCheckbutton', background=farbe[colour]) st1.configure('brown.TLabel', background='#EDEF77') st1.configure('lowr.TLabel', background='lightblue') # new st1.configure('upr.TLabel', background='red') # new self.lf0 = LabelFrame(self.parent, text=self.lf_text, style=self.colour + '.TLabelframe') self.lf0.grid(column=0, row=0, padx=10, pady=10) self.messlbl = Label(self.lf0, style='brown.TLabel') # removed text self.messlbl.grid(row=2, column=0, pady=10, padx=10) self.make_entry() def make_entry(self): """construct of Entry Parameters ---------- None Returns ------- None """ vcmd = self.lf0.register(self.is_okay) self.ent0 = ent0 = Entry(self.lf0, validate='key', validatecommand=(vcmd, '%P', '%S', '%i'), textvariable=self.out_var) ent0.bind("<Return>", self.end_input) ent0.grid(row=1, column=0, padx=10) ent0.focus() if self.mod in (True, False): self.modify() def modify(self): """construct of state switch Parameters ---------- None Returns ------- None """ lf_text = self.lf_text # entry disabled until checkbox is ticked self.cb_opt = Checkbutton(self.lf0, command=self.toggle_opt, style=self.colour + '.TCheckbutton') self.lf0['labelwidget'] = self.cb_opt if self.mod: self.ent0.state(['!disabled']) self.cb_opt.state(['!selected']) self.cb_opt['text'] = lf_text # +'\n Check to prevent editing ' self.ent0.focus() else: self.ent0.state(['disabled']) self.cb_opt.state(['selected']) self.cb_opt['text'] = lf_text # +'\n Check to modify ' def toggle_opt(self): """state switch logic Parameters ---------- None Returns ------- None """ lf_text = self.lf_text # state of entry controlled # by the state of the check button in Option frame label widget if self.cb_opt.instate(['selected']): self.ent0.state(['disabled']) self.cb_opt['text'] = lf_text # +'\n Check to modify ' else: self.ent0.state(['!disabled']) # enable option self.cb_opt['text'] = lf_text # changed self.ent0.focus() def end_input(self, evt): """limit on string Parameters ---------- evt : str bind handle Returns ------- None """ if len(self.out_var.get()) > 5: self.messlbl['text'] = "That's OK" else: self.messlbl['text'] = "Need at least 6 characters" def is_okay(self, text, inp, ind): """ validation function Parameters ---------- text : str text if allowed inp : str current input Returns ------- boolean """ ind = int(ind) print(ind) if (inp.isalnum() or inp in (",", ".", "'", " ")) and ind > 0: return True else: return bool((text.isupper() or text == "") and ind == 0)