gpt4 book ai didi

python - 使用 Kivy 创建 sqlite3 数据库

转载 作者:行者123 更新时间:2023-12-01 08:29:18 24 4
gpt4 key购买 nike

我正在尝试使用 Kivy 在 Python 3 中使用 sqlite3 创建数据库。我在 YouTube、Stack Overflow 上进行了搜索,甚至从 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()

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 (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 内部而是在数据库内部它是两个不同的类......

关于python - 使用 Kivy 创建 sqlite3 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53991436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com