Search code examples
pythonclasstkintertextboxtraceback

Problem with python, POO, tkinter and save problems


This is the code, I hope someone could help me please. I created a class and an object. I want to use a textbox to enter a property of the object. After that i want to use a button to show the property in the window.

from tkinter import *

add_window = Tk()
add_window.resizable(False, True)
add_window.geometry("300x400")
add_window.title("Add a new account")
add_window.configure(bg="Black")


class cuenta:
     app_web = StringVar()


def com(self):
    c = self.app_web.get()
    titulo = Label(text=c, font=20).grid(row=3, column=1)

cuenta1 = cuenta()

app_web = Label(add_window, text="App or web: ", bg="black", fg="white", font=("Segoe UI", 12))
app_web.grid(row=1, column=0)
caja_app = Entry(add_window, textvariable=cuenta1.app_web, bg="grey88", fg="black").grid(row=1, column=1)
boton_save = Button(add_window, text="Save", command=cuenta.com(cuenta1), bg="azure4", 
fg="white").grid(row=2, column=1)

add_window.mainloop()

Here is the error I am getting:

Traceback (most recent call last): File "C:\Users\offcampus\Desktop\bahula.py", line 23, in boton_save = Button(add_window, text="Save", command=cuenta.com(cuenta1), bg="azure4", AttributeError: type object 'cuenta' has no attribute 'com' [Finished in 0.2s with exit code 1] [shell_cmd: python -u "C:\Users\offcampus\Desktop\bahula.py"] [dir: C:\Users\offcampus\Desktop] [path: C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\system32\config\systemprofile.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Users\offcampus\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\offcampus\AppData\Local\Programs\python35\paint.py;C:\Users\offcampus\AppData\Local\Programs\Python\Python35;C:\Users\offcampus\AppData\Local\Programs\Python;]


Solution

  • The problem was that you need to define the com function inside the class cuenta. Instead you defined it outside the class. You need to indent the function in by four spaces.

    Here is the code:

    from tkinter import *
    
    add_window = Tk()
    add_window.resizable(False, True)
    add_window.geometry("300x400")
    add_window.title("Add a new account")
    add_window.configure(bg="Black")
    
    
    class cuenta:
        app_web = StringVar()
        def com(self):
            c = self.app_web.get()
            titulo = Label(text=c, font=20).grid(row=3, column=1)
    
    cuenta1 = cuenta()
    
    app_web = Label(add_window, text="App or web: ", bg="black", fg="white", font=("Segoe UI", 12))
    app_web.grid(row=1, column=0)
    caja_app = Entry(add_window, textvariable=cuenta1.app_web, bg="grey88", fg="black").grid(row=1, column=1)
    boton_save = Button(add_window, text="Save", command=cuenta.com(cuenta1), bg="azure4", 
    fg="white").grid(row=2, column=1)
    
    add_window.mainloop()
    

    Hope this helps!