2

I am using Tkinter to create a GUI. I have a class for setting up all my GUI elements and another class that does some functionality.

class class_one():

    def method_one(self):
        do_something()

class GUI()

    def __init__(self):
        button = Button(self, text="button", command=call_class_one_method)
        button.pack()

    def call_class_one_method(self):
         c = class_one()
         c.method_one()

Is this above code the correct way for calling other class methods or should I be instantiating the class in the GUI's __init__ method? Or perhaps something else?

2 Answers 2

3

In this specific case you should instantiate it in GUI.__init__ unless there's a reason you need to create a new instance every time they click the button.

class GUI()

    def __init__(self):
        self.class_one = class_one()
        button = Button(self, text="button", command=self.class_one.method_one)
        ...
Sign up to request clarification or add additional context in comments.

Comments

0

Unless you actually need a new instance of the class for every button press (which doesn't seem to be the case in your example), you should instantiate it in init.

Comments

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.