2

I got some kind of problem with the structure with my code. I am using Tkinter and building a GUI. My idea is as follows: there is a field that takes the path to a file and when you click Start, Python opens the path and loads the file as a csv. Then it stores the file in a dict with some more information and returns the dict that will be passed around to other functions. However, how can this work? The Button can only run a function but not return something, right?

So I can enter the (partially filled) dict as an argument of this input function, however, it is never returned. My solution until now was to declare it a global, so when it is changed within the function, it is also changed outside. However, now I restructure the code and want to reuse code (since files are imported at several stages in the process). Now, when I want to use the same function again, the global solution seems problematic. Any ideas how I can do this without them? Thanks a lot.

Update1:

class Topclass:
    def changer(x):
        x += 1


class Subclass(Topclass):
    def __init__(self):
        self.b = 2


obb = Subclass()
print(obb.b)
Topclass.changer(obb.b)
print(obb.b)
5
  • Suggest you read answer by Bryan Oakley to Best way to structure a tkinter application?. Commented Jun 1, 2019 at 7:44
  • Thanks but I already did that since all pages are classes and inherit from a TK Master class. I dont see how this solves my problem. See my little test code as an example: (update1) Commented Jun 1, 2019 at 8:05
  • Your question is supposedly about tkinter, but your sample code isn't using it. The question I linked to is about structuring a tkinter application, and if you follow the advice in the accepted answer, it describes how to use classes to do so. If you use classes, you can save values in attributes of that class, and other methods of the class can access those attribute through the self argument they all have. Commented Jun 1, 2019 at 8:16
  • If you pass something mutable as an argument to a function, like a dict or an instance of your own custom class as shown in @Reblochon Masque's answer, you can modify it in the function. Alternatively you could add a method to the custom class that make the change—so there's no need to return the modified value. You can't do this with something immutable like a integer or string value however—the workaround is to make them attributes of class instance, again like @Reblochon is doing. Commented Jun 1, 2019 at 9:38
  • Thank you @martineau, it is always nice to read in the comments that an answer was useful... Commented Jun 14, 2019 at 2:33

1 Answer 1

1

@Martineau's advice is correct, there is much to learn from @BryanOakley's Best way to structure a tkinter application's answer.

Using a DataTransfer specialized object is a technique that you can use to pass variables, or data around, with frameworks that do not return values like tkinter, or when you need your data to cross an encapsulation barrier, without tightly coupling the objects involved:

Here is a simple example to illustrate how that works:

class DataTransfer:
    """mutable object that is used to transfer data
    """
    def __init__(self, value: int) -> None:
        self.value = value

    def add_one(self) -> None:
        self.value += 1

    def __str__(self) -> str:
        return f'{self.value}'


class Topclass:
    def changer(x: DataTransfer) -> None:
        x.add_one()


class Subclass(Topclass):
    def __init__(self) -> None:
        self.b = DataTransfer(2)


obb = Subclass()
print(obb.b)
Topclass.changer(obb.b)
print(obb.b)
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, this is genius! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.