python、POO、tkinter 问题和保存问题

Problem with python, POO, tkinter and save problems

这是代码,希望有人能帮助我。 我创建了一个 class 和一个对象。我想用一个文本框来输入一个 属性 的对象。之后我想用一个按钮在 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()

这是我收到的错误:

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 Server0\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;]

问题是您需要在 class cuenta 中定义 com 函数。相反,您将其定义在 class 之外。您需要将函数缩进四个空格。

代码如下:

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()

希望对您有所帮助!