我是python,kivy和sqlite的新手。但我必须完成这项艰巨的任务。 :-(任何形式的帮助都将受到高度赞赏。提前致谢!
任务是:在android上的.db
屏幕上显示kivy
文件中的数据。
我从http://zetcode.com/db/sqlitepythontutorial/
创建了数据库文件我在这里再次发布代码。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sqlite3 as lite
import sys
con = lite.connect('test.db')
with con:
cur = con.cursor()
cur.execute("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)")
cur.execute("INSERT INTO Cars VALUES(1,'Audi',52642)")
cur.execute("INSERT INTO Cars VALUES(2,'Mercedes',57127)")
cur.execute("INSERT INTO Cars VALUES(3,'Skoda',9000)")
cur.execute("INSERT INTO Cars VALUES(4,'Volvo',29000)")
cur.execute("INSERT INTO Cars VALUES(5,'Bentley',350000)")
cur.execute("INSERT INTO Cars VALUES(6,'Citroen',21000)")
cur.execute("INSERT INTO Cars VALUES(7,'Hummer',41400)")
cur.execute("INSERT INTO Cars VALUES(8,'Volkswagen',21600)")
然后将数据库保存到C:\\test.db
。
然后我使用kivy
和label
制作了button
屏幕。
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.lang import Builder
import random
root_widget = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
Label:
text: 'Hello' #How to define it?
font_size: 30
Button:
size: root.width/2, 15
text: 'next random'
font_size: 30
# on_release: #How to define it?
''')
class TestApp(App):
def build(self):
return root_widget
if __name__ == '__main__':
TestApp().run()
每次点击random
时,我希望db file
上显示label
的{{1}} 车名。
最初的button
文本,现在是“Hello”,应该用随机的汽车名称替换。因此,每次运行程序时,label
上都会显示一个汽车名称。
但我真的不知道如何编写代码。
感谢您的帮助。
#UPDATE
我编写了代码,但它不起作用。
label
#2更新:
现在代码是......它显示错误消息:# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.clock import mainthread
import sqlite3
import random
class MyBoxLayout(BoxLayout):
def init(self, **kwargs):
super().__init__(**kwargs)
@mainthread # execute within next frame
def delayed():
self.load_random_car()
delayed()
def load_random_car(self):
conn = sqlite3.connect("C:\\test.db")
cur = conn.cursor()
####Length of db file
with conn:
cur = conn.cursor()
cur.execute("SELECT * FROM Cars")
rows = cur.fetchall()
LengthSQLFile = len(rows)
print LengthSQLFile
CurrentNo = random.randint(0, LengthSQLFile)
CurrentNo_ForSearch = (CurrentNo ,)
cur.execute("select * from Cars where rowid = ?" , CurrentNo_ForSearch)
CurrentAll = cur.fetchone()
CurrentAll = list(CurrentAll) # Change it from tuple to list
print CurrentAll
Current = CurrentAll[1]
self.ids.label.text = Current #"fetch random car data from db and put here"
root_widget = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
Label:
id: label
font_size: 30
Button:
size: root.width/2, 15
text: 'next random'
font_size: 30
on_release: root.load_random_car()
''')
class TestApp(App):
def build(self):
# MyBoxLayout(BoxLayout)
return root_widget
if __name__ == '__main__':
TestApp().run()
AttributeError: 'BoxLayout' object has no attribute 'load_random_car'
答案 0 :(得分:3)
最简单的方法是为框布局编写自定义init
方法:
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import mainthread
class MyBoxLayout(BoxLayout):
def init(self, **kwargs):
super().__init__(**kwargs)
@mainthread # execute within next frame
def delayed():
self.load_random_car()
delayed()
def load_random_car(self):
self.ids.label.text = "fetch random car data from db and put here"
这是更新的小部件结构的样子:
MyBoxLayout:
Label:
id: label
Button:
on_release: root.load_random_car()