你好,我正在用kivy和python开发移动的应用程序。一切都很好,直到我想用sqlite3。下面是我的目录结构:
application
├───main.py
├───core
│ └───__pycache__
| ├───calendar_screen.py
| ├───children.py
| ├───menu.py
| ├───screen_manager.py
| ├───settings.py
| └───vaccination.py
├───database
| ├───vaccination_calendar.db
| ├───vaccination_calendar.py
│ └───__pycache__
├───fonts
│ └───static
│ ├───OpenSans
│ ├───OpenSans_Condensed
│ └───OpenSans_SemiCondensed
├───images
│ └───icons
├───layouts
| ├───calendar.kv
| ├───children.kv
| ├───menu.kv
| ├───screen_manager.kv
| ├───settings.kv
| └───vaccination.kv
└───__pycache__
相关代码main.py
from kivymd.app import MDApp
from core.screen_manager import WindowManager
class VaccinationCalendarApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
return WindowManager()
if __name__ == "__main__":
VaccinationCalendarApp().run()
屏幕管理器.py
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
Builder.load_file("layouts/screen_manager.kv")
class WindowManager(ScreenManager):
pass
屏幕管理器.kv
#: import Menu core.menu
#: import Calendar core.calendar_screen
#: import Children core.children
#: import Vaccination core.vaccination
#: import Settings core.settings
<WindowManager>:
Menu:
Calendar:
Children:
Vaccination:
SettingsScreen:
children.py
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivymd.uix.list import OneLineAvatarIconListItem, IconLeftWidget
from application.database.vaccination_calendar import get_children
Builder.load_file("layouts/children.kv")
class Children(Screen):
pass
疫苗接种_日历.py
import sqlite3
def get_children():
statement = "select * from children"
with sqlite3.connect("vaccination_calendar.db") as conn:
cursor = conn.cursor()
cursor.execute(statement)
children_list = [{"id": child[0], "name": child[1], "birth_date": child[2], "days_age": child[3]}
for child in cursor.fetchall()]
conn.commit()
return children_list
在www.example.com中出现这一行之前,一切都很正常children.py:from application.database.vaccination_calendar import get_children.添加这一行之后,我得到了带有追溯功能的AttributeError:
Traceback (most recent call last):
File "C:\Users\przem\PycharmProjects\vaccination_calendar\application\main.py", line 3, in <module>
File "C:\Users\przem\PycharmProjects\vaccination_calendar\env\lib\site-packages\kivy\lang\builder.py", line 305, in load_file
return self.load_string(data, **kwargs)
File "C:\Users\przem\PycharmProjects\vaccination_calendar\env\lib\site-packages\kivy\lang\builder.py", line 372, in load_string
parser = Parser(content=string, filename=fn)
File "C:\Users\przem\PycharmProjects\vaccination_calendar\env\lib\site-packages\kivy\lang\parser.py", line 483, in __init__
self.parse(content)
File "C:\Users\przem\PycharmProjects\vaccination_calendar\env\lib\site-packages\kivy\lang\parser.py", line 590, in parse
self.execute_directives()
File "C:\Users\przem\PycharmProjects\vaccination_calendar\env\lib\site-packages\kivy\lang\parser.py", line 559, in execute_directives
mod = getattr(mod, part)
AttributeError: module 'core' has no attribute 'children'
我怀疑这是循环依赖的问题,但我看不出有什么问题。我过去这里的文件是从www.example.com开始的导入顺序main.py。
我尝试通过www.example.com中的方法导入vaccination_calendar.pychildren.py,但之后我收到ModuleNotFoundError,我还尝试导入main.py,在那里它工作正常,但我没有看到任何在儿童屏幕中使用vaccination_calendar.py的想法。
2条答案
按热度按时间cbeh67ev1#
错误可在以下行中找到:
我假设你运行
main.py
,当你运行./application
时,./application
被添加到sys.path
中,这就是为什么Python永远不能解析像application.some.module_path
这样的模块路径,相反,它应该只是some.module_path
部分。这个错误消息非常隐晦,但这是Kivy试图解析
screen_manager.kv
文件的结果,在这个过程中,它发现了这个错误的导入,并在Kivy库内部引发了一个错误(因为它试图解析一个.kv
文件),而不是直接告诉你到底出了什么问题。jljoyd4f2#
我找到了这个问题的解决方案。我不知道问题到底出在哪里,但我改变了导入vaccination_calendar的方式。
对此:
Pycharm仍然看到一个问题,但它对我很有效。也许有人可以使用它。