Я пытаюсь создать базу данных, используя sqlite3 в Python 3, используя Kivy.Я искал по всей YouTube, переполнение стека, и даже купил книгу 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()
Файл KV
#: 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
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'