首页 > 解决方案 > OOP 设计问题 - 'postulantet' 对象没有属性 'tabla_postulante'

问题描述

我在使用 tkinter 和面向对象编程时遇到问题。我有 3 个功能:

  1. 首先,我有v2()一个功能,该功能允许通过按钮打开第二个窗口并显示保存在数据库中的数据

  2. 我的第二个功能是agregar_postulantes(). 此函数将数据保存在数据库中。

  3. 我的第三个函数是保存数据agregar_postulantes()并将其发送到函数的v2()函数。

我想强调的是,所有函数都在同一个类中,并且在__init__().

代码的简短版本:

def v2(self):
    self.tabla_postulante=ttk.Treeview(tablaBD,columns=("Name","last name","id")
    self.tabla_postulante.heading("Name",text="Name")
    self.tabla_postulante.heading("last name",text="last name")
    self.tabla_postulante.heading("id",text="id")

    self.tabla_postulante['show']='headings'
    self.tabla_postulante.column("Name",width=100)
    self.tabla_postulante.column("last name",width=100)
    self.tabla_postulante.column("id",width=100)
    self.fetch_all()
    self.tabla_postulante.pack(fill=BOTH,expand=1)

def agregar_postulantes(self):
    con = pymysql.connect(host="localhost", user="root",password="", database="postulantebd")
    cur = con.cursor()
    cur.execute("insert into postulantes values(%s, %s, %s)",(
            self.name_var.get(),
            self.lastname_var.get(),
            self.id_var.get(),
            ))
    con.commit()
    self.fetch_all()
    con.close()

def fetch_all(self):
    con = pymysql.connect(host="localhost", user="root",password="", database="postulantebd")
    cur = con.cursor()
    cur.execute("select * from postulantes")
    rows=cur.fetchall()
    if len(rows)!=0:
        self.tabla_postulante.delete(*self.tabla_postulante.get_children())
        for row in rows:
            self.tabla_postulante.insert('',END,values=row)
        con.commit()
    con.close()

出现的错误如下:

Traceback (most recent call last):
  File "C:\Users\dimitri\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\dimitri\Documents\Programas de Yolo\practicadeinterfazypoo\tkinter\interfaz_tesis_poo.py", line 273, in agregar_postulantes
    self.fetch_all()
  File "C:\Users\dimitri\Documents\Programas de Yolo\practicadeinterfazypoo\tkinter\interfaz_tesis_poo.py", line 282, in fetch_all
    self.tabla_postulante.delete(*self.tabla_postulante.get_children())
AttributeError: 'postulante' object has no attribute 'tabla_postulante'

错误是因为我在使用其他两个函数保存数据后调用 v2 函数之后如何调用该v2函数而不显示错误?

标签: pythonpython-3.xooptkinter

解决方案


正如AttributeError各州一样,self似乎没有tabla_postulante属性。由于您在函数中创建了一个table_postulante属性,我猜您没有在中定义或者您没有在调用之前调用。selfv2self.table_postulante__init__()v2()fetch_all()

应该意味着您有几个选择:

  1. 创建一些逻辑fetch_all()来检查是否存在self.tabla_postulante并且仅在找到该属性时执行。这可以采用try/except语句的形式。

  2. 或者,在实例化对象时self.tabla_postulante在调用中定义属性。__init__()

更新

关于实现我上面描述的内容的更多细节。我将假设您没有self.tabla_postulante在 中的类上创建属性,因为如果您是,则在函数查找它时__init__()您不会得到 a 。ValueError

所以这给了你几个选择。一种是简单地确保当您实例化您的类时__init__()创建一个self.table_postulante属性,如下所示:

class ClassName:
    def __init__(self):
        #create attribute here
        self.table_postulante = ttk.Treeview(tablaBD,columns=("Name", "last name","id")
        ...

如果这对您正在开发的应用程序没有意义,另一种选择是在fetch_all(). 例如,您可以执行以下操作:

if len(rows)!=0:
    try:
        self.tabla_postulante.delete(*self.tabla_postulante.get_children())
        for row in rows:
            self.tabla_postulante.insert('',END,values=row)
        con.commit()
    except:
        #define some appropriate response to this attribute not existing here

您的用例当然会决定哪种方法最有意义。希望这可以帮助。


推荐阅读