0

I'm new to Python and trying to create an app to perform Psychrometric calculations. I have created a UI using customTkInter and want to retrieve the contents of an entry widget to perform some calculations. I have created a get method from within the class but when I try to call it the interpreter crashes saying there is no reference. From everything I have searched I appear to be doing it right but there must be something amiss. All help appreciated. Error is AttributeError: '_tkinter.tkapp' object has no attribute 'get_db'

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        self.geometry("500x300")
        self.title("Psychrometrics")
        customtkinter.set_appearance_mode("system")
        customtkinter.set_default_color_theme("green")

        label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
        label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)

        entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
        entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)

        label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
        label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)

        entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
        entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)

        label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
        label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)

        button = customtkinter.CTkButton(self, text="Calculate", command=login)
        button.grid(row=3, column=0, pady=DPadY, padx=DPadX)

        def get_db(self):
            _Db = self.entry_db.get()
            return _Db



def login():
    Cdb = app.get_db()
    print(Cdb)

app = App()
app.mainloop()

Tried multiple ways of getting the value and simlply want to print to output the contents.

  • Does this answer your question? [How do I get the Entry's value in tkinter?](https://stackoverflow.com/questions/35662844/how-do-i-get-the-entrys-value-in-tkinter) – Ben the Coder May 13 '23 at 14:20

1 Answers1

1

The first issue is with the indentation of the def get_db(self): method. Indent it backward so it won't be part of the init of the class.

Secondly, in the init method, entry_db should be self.entry_db so you can access it inside the get_db method as well. Then your code will look like this:

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        self.geometry("500x300")
        self.title("Psychrometrics")
        customtkinter.set_appearance_mode("system")
        customtkinter.set_default_color_theme("green")

        label_title = customtkinter.CTkLabel(self, text="Saturation Pressure")
        label_title.grid(row=0, column=0, pady=DPadY, padx=DPadX)

        self.entry_db = customtkinter.CTkEntry(self, placeholder_text="Dry Bulb")
        self.entry_db.grid(row=1, column=0, pady=DPadY, padx=DPadX)

        label_dbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Dry Bulb Saturation Pressure")
        label_dbsp.grid(row=1, column=1, pady=DPadY, padx=DPadX)

        entry_wb = customtkinter.CTkEntry(self, placeholder_text="Wet Bulb")
        entry_wb.grid(row=2, column=0, pady=DPadY, padx=DPadX)

        label_wbsp = customtkinter.CTkEntry(self, width=300, placeholder_text="Wet Bulb Saturation Pressure")
        label_wbsp.grid(row=2, column=1, pady=DPadY, padx=DPadX)

        button = customtkinter.CTkButton(self, text="Calculate", command=login)
        button.grid(row=3, column=0, pady=DPadY, padx=DPadX)

    def get_db(self):
        _Db = self.entry_db.get()
        return _Db

Should work. Do the same to all other entry boxes to be able to print or retrieve their contents.

  • 1
    Thank you, that worked. I can see the principle of indention error, will help me moving forward with how Python sees indentation and spacing. Using self. on Init was In the example I based my code on, I've simply missed it off. Thanks – Paul Dresser May 13 '23 at 12:24