关于python,POO,tkinter和保存问题的问题

问题描述 投票:0回答:1

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

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

这是我得到的错误:

追踪(最近通话): 文件“ C:\ Users \ offcampus \ Desktop \ bahula.py”,第23行,在 boton_save = Button(add_window,text =“ Save”,command = cuenta.com(cuenta1),bg =“ azure4”, AttributeError:类型对象“ cuenta”没有属性“ com” [在0.2秒内完成,退出代码为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;]

python class tkinter textbox traceback
1个回答
0
投票

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

这里是代码:

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

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.