Python Forum
access share attributed among several class methods
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
access share attributed among several class methods
#1
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

Attached Files

Thumbnail(s)
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class test : good way to split methods into several files paul18fr 4 404 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  How can I access objects or widgets from one class in another class? Konstantin23 3 931 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  Timestamp of file changes if a share mapped to a server…. tester_V 34 3,638 Jul-04-2023, 05:19 AM
Last Post: tester_V
  Structuring a large class: privite vs public methods 6hearts 3 1,017 May-05-2023, 10:06 AM
Last Post: Gribouillis
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  dict class override: how access parent values? Andrey 1 1,594 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Access instance of a class Pavel_47 5 2,035 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  a function common to methods of a class Skaperen 7 2,501 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Listing All Methods Of Associated With A Class JoeDainton123 3 2,296 May-10-2021, 01:46 AM
Last Post: deanhystad
  too many methods in class - redesign idea? Phaze90 3 2,453 Mar-05-2021, 09:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020