Python Forum
access share attributed among several class methods - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: access share attributed among several class methods (/thread-35576.html)



access share attributed among several class methods - drSlump - Nov-18-2021

I have developed a GUI using tkinter with python 3.9. It works but I am facing the following problem. When I activate/deactivate one of the defined checkbox, in addition to activate/deactivate the related second entry, I want also activate/deactivate the last single entry ((unit_3 in the attached picture)

# main that calls the modules as md
   for index, (key, values) in enumerate(dictionary.items()):
        obj = md.CreateEntry(input_data_frame, values, index)
        if md.arenumbers(values[-2], values[-1]):
            pass
            obj.double_entry()
        elif md.arenumbers(values[-1]):
            print(values)
            obj.single_entry()
        else:
            print(values)
            obj.labels_entry()
and here the class I am using

class CreateEntry(tk.Frame):
    def __init__(self, parent, values, row, bg=None, **kwargs):
        if bg is None:
            bg = parent['bg']
        super().__init__(parent, bg=bg, **kwargs)
        self.parent = parent
        self.values = values
        self.row = row
        self.nac = tk.IntVar(self.parent)
        self.sim_steps = None
        self.entry_step_state = None

    @property
    def sim_steps(self):
        """I'm the 'var_second_entry' property."""
        return self._sim_steps

    @sim_steps.setter
    def sim_steps(self, value):
        self._sim_steps = tk.StringVar(self.parent, value=value)

    @sim_steps.deleter
    def sim_steps(self):
        del self._sim_steps

    @property
    def entry_step_state(self):
        """I'm the 'var_second_entry' property."""
        return self._entry_step_state

    @entry_step_state.setter
    def entry_step_state(self, value='disabled'):
        self._entry_step_state = value

    def double_entry(self):

        self.variable1 = tk.StringVar(self.parent, value=self.values[-2])
        self.variable2 = tk.StringVar(self.parent, value=self.values[-1])

        tk.Checkbutton(self.parent, variable=self.nac, command=lambda: self.naccheck()).\
            grid(row=self.row, column=0, sticky='ns')

        label = self.values[0] + ' (' + self.values[1] + ') '
        tk.Label(self.parent, text=label, padx=10, pady=5).\
            grid(row=self.row, column=1, sticky='nw')

        self.entry_c1 = tk.Entry(self.parent, textvariable=self.variable1, width=10, state=CreateEntry.entry_step_state)
        self.entry_c1.grid(row=self.row, column=2)

        self.entry_c2 = tk.Entry(self.parent, textvariable=self.variable2, width=10, state='disabled')
        self.entry_c2.grid(row=self.row, column=3)

        self.entry_c1.bind("<KeyRelease>", self._accept)
        self.entry_c2.bind("<KeyRelease>", self._accept)

    def single_entry(self):
        CreateEntry.sim_steps = self.values[-1]
        entry = tk.Entry(self.parent, textvariable=CreateEntry.sim_steps, width=10, state='normal')
        entry.grid(row=self.row + 1, column=2, columnspan=2)
        entry.bind("<KeyRelease>", self._accept)

        tk.Label(self.parent, text='SIMULATION PARAMETERS', padx=10, pady=5).\
            grid(row=self.row, columnspan=4, sticky='ns')
        tk.Label(self.parent, text=self.values[0], padx=10, pady=5).\
            grid(row=self.row + 1, column=0, columnspan=2, sticky='ns')

    def labels_entry(self):
        tk.Label(self.parent, text=self.values[0], padx=10, pady=5).grid(row=self.row, column=1, sticky='ns')
        tk.Label(self.parent, text=self.values[1], padx=10, pady=5).grid(row=self.row, column=2, sticky='ns')
        tk.Label(self.parent, text=self.values[2], padx=10, pady=5).grid(row=self.row, column=3, sticky='ns')

    def naccheck(self):
        if self.nac.get() == 0:
            self.variable2.set(self.variable1.get())
            self.entry_c2.configure(state='disabled')
            del CreateEntry.sim_steps
            CreateEntry.sim_steps = '0'
            CreateEntry.entry_step_state = 'disabled'
        else:
            self.entry_c2.configure(state='normal')
            CreateEntry.entry_step_state = 'normal'

    def _accept(self, event):
        """Accept value change when a key is released"""
        try:
            if self.entry_c1.get():
                float(self.entry_c1.get()) or int(self.entry_c1.get())
            else:
                self.variable1.set('0')
        except ValueError:
            self.bell()
            showerror(title='Title', message='Initial value not numeric!')

        try:
            if self.entry_c2.get():
                float(self.entry_c2.get()) or int(self.entry_c2.get())
                if self.nac.get() == 0:
                    self.values[-1] = self.entry_c1.get()
                else:
                    self.values[-1] = self.entry_c2.get()
                    if self.entry_c2.get() <= self.entry_c1.get():
                        showerror(title='Title', message='Final value not greater than initial value!')
            else:
                self.variable2.set('0')
        except ValueError:
            showerror(title='Title', message='Final value not numeric!')
But the last entry is always deactivated