我有两个文件:test.py
和test.kv
。当我从insert_update_account()
文件中调用.kv
函数时,它会出错:
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/>
如果我对self.display_account()
函数中的行insert_update_account()
发表评论,则表示没有错误。
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()
有人可以帮助我吗?
答案 0 :(得分:2)
在我看来,您在类cur
中混合了光标con
和连接MainMenu
,因为您已在全局范围内定义它,并且也使用相同的名称在班级范围内。因此,这可能会发生,因为变量已在您的代码中自由混合。
您应该尝试在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()