Kivy 滚动视图:属性错误:'NoneType'对象没有属性'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:UsersUserPycharmProjectspythonProject3APPvenvlibsite-packageskivyapp.py", line 949, in run
self._run_prepare()
File "C:UsersUserPycharmProjectspythonProject3APPvenvlibsite-packageskivyapp.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:UsersUserPycharmProjectspythonProject3APPvenvlibsite-packageskivylangbuilder.py", line 408, in load_string
self._apply_rule(
File "C:UsersUserPycharmProjectspythonProject3APPvenvlibsite-packageskivylangbuilder.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类的__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中的行的高度过小。

最新更新