Home > other >  Getting a value after the object was created from user input Tkinter Python
Getting a value after the object was created from user input Tkinter Python

Time:10-27

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

CodePudding user response:

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.

  • Related