Python Forum
[Tkinter] button, how to do many things
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] button, how to do many things
#2
I think I understand your question. Let me rephrase to be sure.

You have this variable(machineProgram.machineClass.rinseTime). You want to get and set the variable's value. You want to be able to do this without having to use the variable's name because you will be expanding your program to not only using rinseTime.

Unfortunately you cannot do what you want to do. There is no "pass by reference" in Python, so you cannot pass a variable to a function and have the function change the variable's value.

What you need are getter and setter functions. These should really be part of your machineClass. MachineClass should have a method for getting the value of rinseTime and another for setting the value of rinseTime. If you are allowed to modify MachineClass you could add these methods. Using @property decorators you could even hide these new methods and make it look like rinseTime is some sort of super variable that supports pass by reference. In MachineClass:
@property
def rinse_time(self):
    return self.rinseTime

@rinse_time.setter
def rinse_time(self, value):
    self.rinseTime = value
If you cannot modify MachineControl you can write the getter and setter functions externally. Inside your GUI module you could write getter's and setters for each machine parameter you want to control.
def set_rinseTime(value):
    machineProgram.machineClass.rinseTime = value

def get_rinseTime():
    return machineProgram.machineClass.rinseTime
Unfortunately I don't think this can be done using lambdas as they don't allow assignment.

The only other way I see around this is sneaky. setAttr sets the value of an attribute in an object. If machineProgram.machineClass is an object, setAttr(machineProgram.machineClass, 'rinseTime', 5) is the same as machineProgram.machineClass.rinseTime = 5.

This is an ugly, ugly, ugly dirty bandaid. The kind of thing you should only ever do when there is no other choice. That doesn't mean it can't be prettied up a bit.
class MachineControl:
    def __init__(self, rinseTime):
        self.rinseTime = rinseTime


class MachineParameter:
    def __init__(self, parent, attribute):
        self.parent = parent
        self.attribute = attribute

    @property
    def value(self):
        return getattr(self.parent, self.attribute)

    @value.setter
    def value(self, new_value):
        setattr(self.parent, self.attribute, new_value)


def incr(param, amount):
    param.value += amount

machine = MachineControl(5)

param = MachineParameter(machine, 'rinseTime')

print('Rinse time =', machine.rinseTime)
incr(param, 1)
print('Rinse time =', machine.rinseTime)
Output:
Rinse time = 5 Rinse time = 6
Of course if you go to the trouble of writing a class, you man as well teach the class how to increment and decrement the parameter value.
class MachineControl:
    def __init__(self, rinseTime):
        self.rinseTime = rinseTime


class MachineParameter:
    def __init__(
        self,
        parent,
        attribute,
        increment=1,
        label=None,
        db=None,
        db_field=None):
        self.parent = parent
        self.attribute = attribute
        self.increment = increment
        self.label = label
        self.db = db
        self.db_field = db_field

    @property
    def value(self):
        return getattr(self.parent, self.attribute)

    @value.setter
    def value(self, new_value):
        setattr(self.parent, self.attribute, new_value)
        if self.db is not None:
            sqlite3Connection.update_setting(
                self.db, self.db_field, self.value)
        if self.label is not None:
            self.label['Text'] = str(self.value)

    def incr(self, count=1):
        self.value += self.increment * count

    def decr(self, count=1):
        self.value -= self.increment * count

machine = MachineControl(5)

param = MachineParameter(machine, 'rinseTime')

print('Rinse time =', machine.rinseTime)
param.incr(2)
print('Rinse time =', machine.rinseTime)
Reply


Messages In This Thread
button, how to do many things - by nanok66 - Apr-24-2020, 07:55 AM
RE: button, how to do many things - by deanhystad - Apr-24-2020, 04:59 PM
RE: button, how to do many things - by nanok66 - Apr-25-2020, 03:21 AM
RE: button, how to do many things - by deanhystad - Apr-25-2020, 03:57 AM
RE: button, how to do many things - by nanok66 - Apr-25-2020, 09:53 PM
RE: button, how to do many things - by deanhystad - Apr-25-2020, 10:34 PM
RE: button, how to do many things - by nanok66 - Apr-25-2020, 10:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,192 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

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