ReferenceError: a weak reference object no longer exists

I have two files: test.pyand test.kv. When I call a insert_update_account()function from a file .kv, then it gives an error:

File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__ (kivy/weakproxy.c:1144)
   File "kivy/weakproxy.pyx", line 26, in kivy.weakproxy.WeakProxy.__ref__ (kivy/weakproxy.c:1043)
 ReferenceError: weakly-referenced object no longer exists<br/>

If I comment out the line self.display_account()in insert_update_account(), then there is no error.

test.py

import kivy

kivy.require('1.9.0')  # replace with your current kivy version !
import sqlite3 as lite
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.lang import Builder

from kivy.core.window import Window
Window.maximize()


con = lite.connect('test.db')
#con = lite.connect(path + 'fact.db')
con.text_factory = str
cur = con.cursor()

class MainMenu(BoxLayout):


    def display_account(self):
        self.dropdown.dismiss()
        self.remove_widgets()
        self.rvaccount = TEST()
        self.content_area.add_widget(self.rvaccount)
        self.cur.close()
        self.con.close()

    def insert_update_account(self, obj):
        cur.execute("UPDATE table SET test1=?, test2=? WHERE test3=?",
                    (obj.col_data[1], obj.col_data[2], obj.col_data[0]))
        con.commit()
        self.display_account()


class TEST(BoxLayout):
    data_items = ListProperty([])
    col1 = ListProperty()
    col2 = ListProperty()

    mode = StringProperty("")

    def __init__(self, **kwargs):
        super(TEST, self).__init__(**kwargs)
        self.get_data()

    def update(self):
        self.col1 = [{'test1': str(x[0]), 'test2': str(x[1]), 'key': 'test1'} for x in self.data_items]
        self.col2 = [{'test1': str(x[0]), 'test2': str(x[1]), 'key': 'test2'} for x in self.data_items]


    def get_data(self):

        cur.execute("SELECT * from table")
        rows = cur.fetchall()
        print(rows)
        i = 0
        for row in rows:
            self.data_items_city.append([row[0], row[1], i])
            i += 1
        print(self.data_items_city)
        self.update()

class TestApp(App):
    title = "test"

    def build(self):
        self.root = Builder.load_file('test.kv')
        return MainMenu()



if __name__ == '__main__':
    TestApp().run()

Can someone help me?

+4
source share
1 answer

It seems to me that you are mixing the cursor curand the join conin your class MainMenu, since you defined it in the global scope and also use the same names within the class.Thus, this can happen because the variables were freely mixed in your code.

MainMenu. - , .

class MainMenu(BoxLayout):

    def __init__(self):
        super(MainMenu, self).__init__(self)
        self.con = lite.connect('test.db')
        self.cur = con.cursor()

    def display_account(self):
        self.dropdown.dismiss()
        self.remove_widgets()
        self.rvaccount = TEST()
        self.content_area.add_widget(self.rvaccount)
        self.cur.close()
        self.con.close()

    def insert_update_account(self, obj):
        self.cur.execute("UPDATE table SET test1=?, test2=? WHERE test3=?",
                    (obj.col_data[1], obj.col_data[2], obj.col_data[0]))
        self.con.commit()
        self.display_account()
+2

Source: https://habr.com/ru/post/1690317/


All Articles