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
  Access instance of a class Pavel_47 5 469 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  a function common to methods of a class Skaperen 7 717 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Listing All Methods Of Associated With A Class JoeDainton123 3 1,053 May-10-2021, 01:46 AM
Last Post: deanhystad
  too many methods in class - redesign idea? Phaze90 3 1,213 Mar-05-2021, 09:01 PM
Last Post: deanhystad
  Special Methods in Class Nikhil 3 1,068 Mar-04-2021, 06:25 PM
Last Post: Nikhil
  How to share a numpy array between 2 processes on Windows? qstdy 0 963 Jan-29-2021, 04:24 AM
Last Post: qstdy
  Can we access instance variable of parent class in child class using inheritance akdube 3 7,196 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Access Network SMB Share on a NAS deltapy 2 3,071 Oct-31-2020, 09:14 AM
Last Post: deltapy
  Trying to access next element using Generator(yield) in a Class omm 2 890 Oct-19-2020, 03:36 PM
Last Post: omm
  cant able to make methods interact with each other in the class jagasrik 2 802 Sep-16-2020, 06:52 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