Kivy Scrollview: AttributeError: 'NoneType' object has no attribute 'bind'
Kivy Scrollview: AttributeError: 'NoneType' object has no attribute 'bind'
早上好,
我是这个网站的新手,但我经常控制有助于我编码的主题。我刚开始学习编码,我是 python 的新手,我认为它是最好的之一。
顺便说一句:我正在研究 Kivy,以构建一个由用户可以收集和查询的卡片组成的应用程序。我构建了卡片(使用 MDCard),但屏幕被挡住了,如果我添加超过 5 张卡片,它们是不可见的(在屏幕外)。我正在尝试在 KV 的 GridLayout 上添加滚动视图。按照一些主题我找到了这种方式。
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.app import runTouchApp
from kivy.uix.button import Button
KV = """
Screen:
Controller:
layout_content: layout_content
BoxLayout:
id: bl
orientation: 'vertical'
padding: 10, 10
row_default_height: '48dp'
row_force_default: True
spacing: 10, 10
ScrollView:
size: self.size
GridLayout:
id: layout_content
size_hint_y: None
cols: 1
row_default_height: '20dp'
row_force_default: True
spacing: 0, 0
padding: 0, 0
MDCard:
id: tel1
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Tele 1"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del primo"
MDCard:
id: tel2
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Tele 2"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del secondo"
MDCard:
id: tel2
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Tele 3"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del terzo"
MDCard:
id:tel4
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Telefilm 4"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del quarto"
MDCard:
id:tel5
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Tele 5"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del quinto"
"""
class Controller(FloatLayout):
layout_content = ObjectProperty(None)
def __init__(self, **kwargs):
super(Controller, self).__init__(**kwargs)
self.layout_content.bind(minimum_height=self.layout_content.setter('height'))
class MainApp(MDApp):
def build(self):
self.title = "LukeFlix"
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "Red"
return Builder.load_string(KV)
MainApp().run()
但是我得到这个错误:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/pythonProject3APP/main.py", line 140, in <module>
MainApp().run()
File "C:\Users\User\PycharmProjects\pythonProject3APP\venv\lib\site-packages\kivy\app.py", line 949, in run
self._run_prepare()
File "C:\Users\User\PycharmProjects\pythonProject3APP\venv\lib\site-packages\kivy\app.py", line 919, in _run_prepare
root = self.build()
File "C:/Users/User/PycharmProjects/pythonProject3APP/main.py", line 132, in build
return Builder.load_string(KV)
File "C:\Users\User\PycharmProjects\pythonProject3APP\venv\lib\site-packages\kivy\lang\builder.py", line 408, in load_string
self._apply_rule(
File "C:\Users\User\PycharmProjects\pythonProject3APP\venv\lib\site-packages\kivy\lang\builder.py", line 659, in _apply_rule
child = cls(__no_builder=True)
File "C:/Users/User/PycharmProjects/pythonProject3APP/main.py", line 125, in __init__
self.layout_content.bind(minimum_height=self.layout_content.setter('height'))
AttributeError: 'NoneType' object has no attribute 'bind'
Process finished with exit code 1
我该如何解决?
错误是由Controller
class的__init__()
方法引起的。此时,layout_content
属性 尚未分配。您可以通过像这样重新定义 Controller
来消除 __init__()
方法:
class Controller(FloatLayout):
pass
然后通过修改 kv
:
的 ScrollView
部分来实现相同的预期结果
ScrollView:
# size: self.size # this has no effect
GridLayout:
id: layout_content
size_hint_y: None
cols: 1
# row_default_height: '20dp'
# row_force_default: True
height: self.minimum_height
spacing: 0, 0
padding: 0, 0
请注意 height: self.minimum_height
行,它执行您的 __init__()
方法试图执行的操作。此外,行默认值迫使 GridLayout
中的行高度过小。
早上好,
我是这个网站的新手,但我经常控制有助于我编码的主题。我刚开始学习编码,我是 python 的新手,我认为它是最好的之一。
顺便说一句:我正在研究 Kivy,以构建一个由用户可以收集和查询的卡片组成的应用程序。我构建了卡片(使用 MDCard),但屏幕被挡住了,如果我添加超过 5 张卡片,它们是不可见的(在屏幕外)。我正在尝试在 KV 的 GridLayout 上添加滚动视图。按照一些主题我找到了这种方式。
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.app import runTouchApp
from kivy.uix.button import Button
KV = """
Screen:
Controller:
layout_content: layout_content
BoxLayout:
id: bl
orientation: 'vertical'
padding: 10, 10
row_default_height: '48dp'
row_force_default: True
spacing: 10, 10
ScrollView:
size: self.size
GridLayout:
id: layout_content
size_hint_y: None
cols: 1
row_default_height: '20dp'
row_force_default: True
spacing: 0, 0
padding: 0, 0
MDCard:
id: tel1
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Tele 1"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del primo"
MDCard:
id: tel2
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Tele 2"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del secondo"
MDCard:
id: tel2
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Tele 3"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del terzo"
MDCard:
id:tel4
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Telefilm 4"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del quarto"
MDCard:
id:tel5
orientation: "vertical"
padding: "8dp"
size_hint: None, None
size: "180dp", "280dp"
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDLabel:
text: "Tele 5"
theme_text_color: "Secondary"
size_hint_y: None
height: self.texture_size[1]
MDSeparator:
height: "5dp"
width: "5dp"
MDLabel:
text: "Descrizione del quinto"
"""
class Controller(FloatLayout):
layout_content = ObjectProperty(None)
def __init__(self, **kwargs):
super(Controller, self).__init__(**kwargs)
self.layout_content.bind(minimum_height=self.layout_content.setter('height'))
class MainApp(MDApp):
def build(self):
self.title = "LukeFlix"
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "Red"
return Builder.load_string(KV)
MainApp().run()
但是我得到这个错误:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/pythonProject3APP/main.py", line 140, in <module>
MainApp().run()
File "C:\Users\User\PycharmProjects\pythonProject3APP\venv\lib\site-packages\kivy\app.py", line 949, in run
self._run_prepare()
File "C:\Users\User\PycharmProjects\pythonProject3APP\venv\lib\site-packages\kivy\app.py", line 919, in _run_prepare
root = self.build()
File "C:/Users/User/PycharmProjects/pythonProject3APP/main.py", line 132, in build
return Builder.load_string(KV)
File "C:\Users\User\PycharmProjects\pythonProject3APP\venv\lib\site-packages\kivy\lang\builder.py", line 408, in load_string
self._apply_rule(
File "C:\Users\User\PycharmProjects\pythonProject3APP\venv\lib\site-packages\kivy\lang\builder.py", line 659, in _apply_rule
child = cls(__no_builder=True)
File "C:/Users/User/PycharmProjects/pythonProject3APP/main.py", line 125, in __init__
self.layout_content.bind(minimum_height=self.layout_content.setter('height'))
AttributeError: 'NoneType' object has no attribute 'bind'
Process finished with exit code 1
我该如何解决?
错误是由Controller
class的__init__()
方法引起的。此时,layout_content
属性 尚未分配。您可以通过像这样重新定义 Controller
来消除 __init__()
方法:
class Controller(FloatLayout):
pass
然后通过修改 kv
:
ScrollView
部分来实现相同的预期结果
ScrollView:
# size: self.size # this has no effect
GridLayout:
id: layout_content
size_hint_y: None
cols: 1
# row_default_height: '20dp'
# row_force_default: True
height: self.minimum_height
spacing: 0, 0
padding: 0, 0
请注意 height: self.minimum_height
行,它执行您的 __init__()
方法试图执行的操作。此外,行默认值迫使 GridLayout
中的行高度过小。