Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 面向对象设计问题&x27;Positantet';对象没有属性';表7;_Python_Python 3.x_Oop_Tkinter - Fatal编程技术网

Python 面向对象设计问题&x27;Positantet';对象没有属性';表7;

Python 面向对象设计问题&x27;Positantet';对象没有属性';表7;,python,python-3.x,oop,tkinter,Python,Python 3.x,Oop,Tkinter,我在使用tkinter和面向对象编程时遇到问题。 我有3个功能: 首先,我有v2()函数,该函数允许通过按钮打开第二个窗口并显示保存在数据库中的数据 我的第二个函数是agregar\u postatantes()。此功能将数据保存在数据库中 我的第三个函数是保存agregar\u postatantes()的数据并将其发送到v2()函数的函数 我想强调的是,所有函数都在同一个类中,并且在\uuuu init\uuuu()之外声明 代码的简短版本: def v2(self): sel

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

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

  • 我的第二个函数是agregar\u postatantes()。此功能将数据保存在数据库中

  • 我的第三个函数是保存agregar\u postatantes()的数据并将其发送到
    v2()
    函数的函数

  • 我想强调的是,所有函数都在同一个类中,并且在
    \uuuu init\uuuu()
    之外声明

    代码的简短版本:

    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
    函数而不显示错误?

    正如
    AttributeError
    所述,
    self
    没有
    tabla\u postatement
    属性。由于您在
    v2
    函数中的
    self
    上创建了
    table\u positionante
    属性,我猜您在调用
    fetch\u all()
    之前没有定义
    self.table\u positionante
    ,或者没有调用
    v2()

    这意味着你有几个选择:

  • fetch\u all()
    中创建一些逻辑,检查是否存在
    self.tabla\u postatante
    并仅在找到该属性时执行。这可以采用
    try
    /
    的形式,但
    语句除外

  • 或者,在实例化对象时,在对
    \uuuu init\uuuu()的调用中定义
    self.tabla\u postatante
    属性

  • 更新 关于实现上述内容的更多细节。我假设您没有在类的
    \uu init\uuu()
    中创建
    self.tabla\u postatante
    属性,因为如果您是,当函数查找它时,您不会得到
    ValueError

    这给了你几个选择。一种方法是确保在实例化类时,
    \uu init\uu()
    创建一个
    self.table\u postatante
    属性,如下所示:

    类名:
    定义初始化(自):
    #在此处创建属性
    self.table_postatante=ttk.Treeview(tablaBD,columns=(“Name”、“last Name”、“id”)
    ...
    
    如果这对您正在开发的应用程序没有意义,另一种选择是在
    fetch\u all()
    中执行一些异常处理。例如,您可以执行以下操作:

    如果len(行)!=0:
    尝试:
    self.tabla\u postatante.delete(*self.tabla\u postatante.get\u children())
    对于行中的行:
    self.tabla_postatante.insert(“”,END,values=row)
    con.commit()
    除:
    #对此处不存在的此属性定义一些适当的响应
    

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

    您需要在其他方法使用它之前调用创建
    tabla_postatement
    v2
    。从这个示例中不清楚是否进行了调用。错误消息不清楚吗?例如,您了解是什么吗e> 属性
    是吗?我认为tabla_postatante是v2中声明的属性。你是对的,我在使用第一个函数之前调用了第二个和第三个函数。你能更具体一点进行验证吗?我绝对是初学者!我会把它放在答案中,这样我就可以利用降价使它更漂亮。