python - 使用 Kivy 创建 sqlite3 数据库
问题描述
我正在尝试使用 Kivy 在 Python 3 中使用 sqlite3 创建一个数据库。我搜索了整个 youtube,堆栈溢出,甚至从 O'Reilly 买了一本 Kivy 书来解决这个问题。我还尝试过使用 tkinter、MySQL 和 JSON 来尝试使该功能正常工作……没有骰子。任何建议都会很棒。先感谢您
它应该做什么 一个简单的输入框来更新和检索学生数据
当我输入数据并尝试更新我得到的数据库时会发生什么:
AttributeError: 'CreateProfile' object has no attribute 'update_database'
代码
import sqlite3
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.checkbox import CheckBox
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.dropdown import DropDown
from kivy.uix.behaviors.button import ButtonBehavior
from kivy.uix.boxlayout import BoxLayout
conn = sqlite3.connect('student.db')
c = conn.cursor()
class Database():
def __init__(self):
super(Database, self).__init__()
def create_table(self):
c.execute(
"CREATE TABLE IF NOT EXISTS studentdata (firstname TEXT, middleinitial TEXT, lastname TEXT, studentid REAL)")
conn.commit()
def read_from_db(self):
c.execute('SELECT * FROM studentdata')
data = c.fetchall()
def update_database(self):
with conn:
c.execute("INSERT INTO studentdata(firstname, middleinitial, lastname, studentid)\
VALUES(?, ?, ?, ?)",
(str(self.firstname.get()),
str(self.lastname.get()),
str(self.middleinitial.get()),
str(self.studentid.get()),
)
)
c.execute('UPDATE studentdata')
conn.commit()
class MainScreen(Screen):
pass
class CreateProfile(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("formfiller.kv")
class FormFiller(App):
title = "FormFiller"
def build(self):
return presentation
if __name__ == '__main__':
Database()
FormFiller().run()
c.close()
conn.close()
千伏文件
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition:FadeTransition()
MainScreen:
CreateProfile:
<Button@Button>:
font_size: 20
<MainTitle@Label>:
font_size: 50
size_hint_y: 1.9
size_hint_x: 1
bold: True
<SubTitle@Label>:
font_size: 25
size_hint_y: 1.70
size_hint_x: 1
bold: True
<InputField@Label>:
font_size: 18
bold: True
<TextInput@TextInput>:
font_size: 16
bold: True
height: self.line_height
<update_database>:
<MainScreen>:
name: "main"
MainTitle:
id: main_title
text: "Packet Filler"
SubTitle:
text: "Making Your Life Easier With the Push of a Button!"
Button:
on_release: app.root.current = "create_profile"
text: "Create New Profile"
pos_hint: {'x':.15, 'y':.45}
size_hint: (.25, .1)
Button:
text: "Load Profile"
pos_hint: {'x':.65, 'y':.45}
size_hint: (.25, .1)
<CreateProfile>:
name: "create_profile"
MainTitle:
id: main_title
text: "Create New Profile"
SubTitle:
text: "Input Your Data"
Button:
text: "Create"
pos_hint: {'x':.15, 'y':.02}
size_hint: (.25, .1)
on_release: root.update_database()
InputField:
text: "First Name:"
size_hint_y: 1.35
size_hint_x: .15
TextInput:
size_hint_y: .05
size_hint_x: .15
multiline: False
pos_hint: {'x':.15, 'y':.65}
InputField:
text: "Middle Initial:"
size_hint_y: 1.2
size_hint_x: .15
TextInput:
size_hint_y: .05
size_hint_x: .15
multiline: False
pos_hint: {'x':.15, 'y':.57}
InputField:
text: "Last Name:"
size_hint_y: 1.05
size_hint_x: .15
TextInput:
size_hint_y: .05
size_hint_x: .15
multiline: False
pos_hint: {'x':.15, 'y':.50}
追溯
Traceback (most recent call last):
File "/home/jarren/PycharmProjects/BCO_Form_Filler/formfiller.py", line 70, in <module>
FormFiller().run()
File "/usr/lib/python3/dist-packages/kivy/app.py", line 826, in run
runTouchApp()
File "/usr/lib/python3/dist-packages/kivy/base.py", line 502, in runTouchApp
EventLoop.window.mainloop()
File "/usr/lib/python3/dist-packages/kivy/core/window/window_sdl2.py", line 727, in mainloop
self._mainloop()
File "/usr/lib/python3/dist-packages/kivy/core/window/window_sdl2.py", line 460, in _mainloop
EventLoop.idle()
File "/usr/lib/python3/dist-packages/kivy/base.py", line 340, in idle
self.dispatch_input()
File "/usr/lib/python3/dist-packages/kivy/base.py", line 325, in dispatch_input
post_dispatch_input(*pop(0))
File "/usr/lib/python3/dist-packages/kivy/base.py", line 291, in post_dispatch_input
wid.dispatch('on_touch_up', me)
File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "/usr/lib/python3/dist-packages/kivy/uix/behaviors/button.py", line 179, in on_touch_up
self.dispatch('on_release')
File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
File "/usr/lib/python3/dist-packages/kivy/lang/builder.py", line 64, in custom_callback
exec(__kvlang__.co_value, idmap)
File "/home/jarren/PycharmProjects/BCO_Form_Filler/formfiller.kv", line 68, in <module>
on_release: root.update_database()
File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__
AttributeError: 'CreateProfile' object has no attribute 'update_database'
解决方案
错误是 CreateProfile 中没有名称为 Update_database 的函数...您有一个类名 Update_database 但不在 createprofile 内部是在数据库内部它的两个不同类....
推荐阅读
- android - 使用 AsyncTask 将图像从 URL 保存到 SQLITE 数据库
- java - 类文件编辑器,我的任何文件都找不到源
- github - 使用 GitHub 存储库中的所有降价文件生成目录
- .net - AJAX 调用的 .NET WebAPI CORS 设置
- javascript - 使用 Google App Script 的 HTML 表单中的动态下拉列表
- php - 如何检查包含 HTML 字符的条目的数组?
- swift - 从 NSTable Row 向 NSTextField 插入字符串
- notepad++ - 将第一个小写转换为大写,将大写转换为小写(正则表达式?)
- docker - 我可以在 docker 标签中使用环境变量吗?
- c# - ASP.NET Core MVC Web 应用程序中的 HTML 编码问题