None 不可调用 KivyMD
None is not callable KivyMD
我正在尝试从动态添加的 'onelineavatarlistbuttons' 文件中的 python 文件切换到我的代码屏幕
但我无法改变他们的屏幕
我的代码
def on_start(self):
list_item = ObjectProperty
list_item = []
self.connection = sqlite3.connect('friend_list.db')
self.cursor = self.connection.cursor()
self.cursor.execute("""SELECT * FROM friend_list ;""")
self.connection.commit()
for name in self.cursor.fetchall():
list_item = [name]
button = OneLineAvatarIconListItem(text = str(name).strip('(').strip(')').strip(" ' ").strip(",").strip("'"),on_press = self.change_screen("Chat_Screen"))
self.root.ids["Chat_List"].ids["list"].add_widget(button)
if name not in list_item:
a = list_item[-1]
button = OneLineAvatarIconListItem(text = str(a).strip('(').strip(')').strip(" ' ").strip(",").strip("'"),on_press = self.change_screen("Chat_Screen"))
self.root.ids["Chat_List"].ids["list"].add_widget(button)
def change_screen(self,screen_name):
screen_manager = self.root.ids['screen_manager']
screen_manager.current = screen_name
on_press
需要不带 ()
且不带参数的函数名称 - 所谓的 "callback"
- 当你按下按钮时,它会使用 ()
来执行这个函数。它还发送 clicked widget
作为参数,这样你就可以识别哪个按钮被点击了,你可以得到 ie。来自小部件的文本。
如果您必须使用参数来赋值函数,那么您可能需要为此使用 lambda
。因为它
on_press=lambda widget:self.change_screen("Chat_Screen")
或发送 widget
到函数
on_press=lambda widget:self.change_screen("Chat_Screen", widget)
def change_screen(self, screen_name, widget):
print(widget)
# ... code ..
现在你的代码在开始时执行函数,它的工作方式类似于
result = self.change_screen("Chat_Screen")
on_press=result
因为 change_screen
不使用 return
所以它 returns None
你得到
on_press=None
我正在尝试从动态添加的 'onelineavatarlistbuttons' 文件中的 python 文件切换到我的代码屏幕 但我无法改变他们的屏幕
我的代码
def on_start(self):
list_item = ObjectProperty
list_item = []
self.connection = sqlite3.connect('friend_list.db')
self.cursor = self.connection.cursor()
self.cursor.execute("""SELECT * FROM friend_list ;""")
self.connection.commit()
for name in self.cursor.fetchall():
list_item = [name]
button = OneLineAvatarIconListItem(text = str(name).strip('(').strip(')').strip(" ' ").strip(",").strip("'"),on_press = self.change_screen("Chat_Screen"))
self.root.ids["Chat_List"].ids["list"].add_widget(button)
if name not in list_item:
a = list_item[-1]
button = OneLineAvatarIconListItem(text = str(a).strip('(').strip(')').strip(" ' ").strip(",").strip("'"),on_press = self.change_screen("Chat_Screen"))
self.root.ids["Chat_List"].ids["list"].add_widget(button)
def change_screen(self,screen_name):
screen_manager = self.root.ids['screen_manager']
screen_manager.current = screen_name
on_press
需要不带 ()
且不带参数的函数名称 - 所谓的 "callback"
- 当你按下按钮时,它会使用 ()
来执行这个函数。它还发送 clicked widget
作为参数,这样你就可以识别哪个按钮被点击了,你可以得到 ie。来自小部件的文本。
如果您必须使用参数来赋值函数,那么您可能需要为此使用 lambda
。因为它
on_press=lambda widget:self.change_screen("Chat_Screen")
或发送 widget
到函数
on_press=lambda widget:self.change_screen("Chat_Screen", widget)
def change_screen(self, screen_name, widget):
print(widget)
# ... code ..
现在你的代码在开始时执行函数,它的工作方式类似于
result = self.change_screen("Chat_Screen")
on_press=result
因为 change_screen
不使用 return
所以它 returns None
你得到
on_press=None