python、POO、tkinter和save问题

2024-09-28 05:37:31 发布

您现在位置:Python中文网/ 问答频道 /正文

这是代码,希望有人能帮我。 我创建了一个类和一个对象。我想使用文本框输入对象的属性。之后,我想使用一个按钮来显示窗口中的属性

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


Tags: textcomaddwebappwindowscolumnwindow
1条回答
网友
1楼 · 发布于 2024-09-28 05:37:31

问题是您需要在类cuenta内定义com函数。而是在类之外定义它。您需要将函数缩进四个空格

代码如下:

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

希望这有帮助

相关问题 更多 >

    热门问题