2

I cannot figure this out even though I watched videos about Object Oriented Programming and Classes in python.

Simplified scenario: 2 classes. class1 is basically a window with two buttons (button1 creates an object of 2nd class and button2 gets the value from inside of this newly created object Entry widget via entrybox.get())

class2 is a frame with tkinter Entry widget on it.

import tkinter as tk

class WindowClass:

    def create_frame(self):
        new_window = FrameClass(self.master)

    def get_value(self):
        """WHAT CODE DO I PUT HERE TO GET THE VALUE ENTERED BY USER INTO THE ENTRYBOX?"""
        pass

    def __init__(self):
        super().__init__()

        root = tk.Tk()
        self.master = root
        self.master.geometry('500x500')

        self.create_frame_button = tk.Button(self.master,
                                             text='Create Frame with Entry',
                                             width=20,
                                             command=self.create_frame)

        self.get_value_button = tk.Button(self.master,
                                          text='Get value from frame',
                                          width=20,
                                          command=self.get_value)

        self.create_frame_button.place(x=10, y=10)
        self.get_value_button.place(x=10, y=40)

        root.mainloop()


class FrameClass:

    def __init__(self, master):

        self.master = master

        self.frame = tk.Frame(self.master, height=250, width=480, relief='solid', bd=4)
        self.entrybox = tk.Entry(self.frame, width=15, font='Calibri, 12')

        self.entrybox.place(x=10, y=10)
        self.frame.place(x=10, y=100)

if __name__ == '__main__':
    WindowClass()

please help, thank you all

3
  • Please follow spacing between equal signs and other measures to make your code more readable as specified by PEP8 (peps.python.org/pep-0008). Others may frown at you for not following this. It is a must-read for intermediate programmers. Commented Oct 26, 2022 at 7:44
  • I appreciate the tip, but PyCharm uses this type of syntax and I find it easy to read as well. Also PyCharm is probably one of my favorite IDE's other than Visual Studio for C variations. Commented Oct 26, 2022 at 23:27
  • that being said the link you provided is an excellent read, thank you! Commented Oct 26, 2022 at 23:56

1 Answer 1

1

Solution

This is a topic I also used to struggle on. But luckily, I found a way to do this. First, make the FrameClass inherit the WindowClass. Next, in the __init__ function of the FrameClass, initialize the WindowClass. Now, when you want to access an attribute of FrameClass in the WindowClass use self. This works because when you initialize the WindowClass in the FrameClass, the FrameClass passes its object (In the code below it is 'obj') into the WindowClass (self refers to the object through which the method was called). Next, if you want to access an attribute of the WindowClass in the FrameClass, you can either use self or super. super makes things faster but can only be used for methods and self searches in the MRO(First child class, then parent class) and can be used for attributes too. Here is your code to make things simpler to understand:

Code

import tkinter as tk

class WindowClass:
    def get_value(self):
        value = self.entrybox.get()
        print(value)
        "DO WHATEVER YOU WANT TO DO WITH THE VALUE HERE"

    def __init__(self):
        root = tk.Tk()
        self.master = root
        self.master.geometry('500x500')

        self.create_frame_button = tk.Button(self.master,
                                             text='Create Frame with Entry',
                                             width=20,
                                             command=lambda: self.create_frame(self.master))

        self.get_value_button = tk.Button(self.master,
                                          text='Get value from frame',
                                          width=20,
                                          command=self.get_value)

        self.create_frame_button.place(x=10, y=10)
        self.get_value_button.place(x=10, y=40)

        root.mainloop()

class FrameClass(WindowClass):
    def __init__(self):
        super().__init__()

    def create_frame(self, master):
        self.__master = master

        self.frame = tk.Frame(self.__master, height=250, width=480, relief='solid', bd=4)
        self.entrybox = tk.Entry(self.frame, width=15, font='Calibri, 12')

        self.entrybox.place(x=10, y=10)
        self.frame.place(x=10, y=100)

if __name__ == '__main__':
    obj = FrameClass()

Note: It is usually recommended to create the GUI in another function not in __init__. I have removed the create_frame function and replaced it with a lambda function. I have also used name mangling to avoid confusion between the self.master of both the classes. I hope this helped you to solve your problem and increased your knowledge of OO Tkinter! Also, if you couldn't understand anything about the information given in the answer, you can comment and ask.

Sign up to request clarification or add additional context in comments.

3 Comments

How interesting, thank you for your quick answer. Do you know any books or maybe internet resources about more advanced OOP and inheritances?
I also realized that by nesting classes this was 100% easier to achieve, but what a great lesson neverless.
Well, you have to go thru many so I recommend top sites such as geeksforgeeks, programiz and w3schools for more reading but I don't know any books (I don't even use them)

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.