python - Kivy AttributeError:“NoneType”对象没有属性“get_screen”
问题描述
我正在尝试根据对我的数据库的查询结果进行Screen
初始化,Buttons
但我得到了一个AttributeError
. 我认为这是因为当我初始化我的应用程序时没有Information Screen
并阅读此答案:AttributeError: 'NoneType' object has no attribute 'current'
我需要用它Clock
来延迟我的类的初始化,Finder
所以我的Information
类有时间被创建但是我不太确定如何在__init__
方法中执行此操作,或者这是否是正确的方法?
class Finder(Screen):
layout_profiles = ObjectProperty(None)
def __init__(self, **kwargs):
super(Finder, self).__init__(**kwargs)
self.mydb = mysql.connector.connect(host="localhost", user="root", passwd="", database="")
self.cur = self.mydb.cursor(buffered=True)
self.cur.execute("SELECT gender, age, name FROM users WHERE location = %s AND date_of_visit = %s",
(self.manager.get_screen('information').location.text,
self.manager.get_screen('information').date))
self.mydb.commit()
self.results = cur.fetchall()
self.cur.close()
self.mydb.close()
if self.results:
for result in self.results:
if result[0] == "male":
male_btn = MaleButton(text="{}, {}".format(result[1], result[2]))
self.layout_profiles.add_widget(male_btn)
elif result[0] == "female":
female_btn = FemaleButton(text="{}, {}".format(result[1], result[2]))
self.layout_profiles.add_widget(female_btn)
else:
unknown_btn = NoGenderButton(text="{}, {}".format(result[1], result[2]))
self.layout_profiles.add_widget(unknown_btn)
错误
line 249, in __init__
(self.manager.get_screen('information').location.text,
AttributeError: 'NoneType' object has no attribute 'get_screen'
解决方案
, dt这是一种在屏幕初始化后执行所有东西的方法
class Finder(Screen):
layout_profiles = ObjectProperty(None)
def __init__(self, **kwargs):
super(Finder, self).__init__(**kwargs)
self.mydb = mysql.connector.connect(host="localhost", user="root", passwd="", database="")
self.cur = self.mydb.cursor(buffered=True)
Clock.schedule_once(self.do_stuff)
def do_stuff(self, dt):
self.cur.execute("SELECT gender, age, name FROM users WHERE location = %s AND date_of_visit = %s",
(self.manager.get_screen('information').location.text,
self.manager.get_screen('information').date))
self.mydb.commit()
self.results = self.cur.fetchall()
self.cur.close()
self.mydb.close()
if self.results:
for result in self.results:
if result[0] == "male":
male_btn = MaleButton(text="{}, {}".format(result[1], result[2]))
self.layout_profiles.add_widget(male_btn)
elif result[0] == "female":
female_btn = FemaleButton(text="{}, {}".format(result[1], result[2]))
self.layout_profiles.add_widget(female_btn)
else:
unknown_btn = NoGenderButton(
text="{}, {}".format(result[1], result[2]))
self.layout_profiles.add_widget(unknown_btn)
推荐阅读
- typescript - 减去两个数组的大小
- mongodb - 您可以在 mongodb Charts 中创建图表或编写聚合查询,以便根据已聚合的字段计算字段吗?
- linux - SOX 异常退出,好像用户正在取消
- javascript - event 和 $event 之间的区别
- python - Python导入消息模块不起作用
- flutter - 每次更新数据时如何在颤振中重建完整视图
- javascript - 如何创建一个将新元素添加到空数组(JS)中的函数?
- r - 如何确保 3 个单独的 dfs 只包含相同的列?
- c# - 在 docker-compose 中访问 GRPC 服务
- git - `export GPG_TTY=$(tty)` 的签名问题