如何解决Kivy Scrollview:AttributeError:'NoneType'对象没有属性'bind'
早上好
我是这个网站的新手,但我不断控制有助于编码的主题。我刚刚开始学习编码,而我对python还是陌生的,我认为它是最好的之一。
顺便说一句:我正在Kivy上开发由用户可以收集和咨询的卡片组成的应用程序。我建立了卡(使用MDCards),但屏幕被遮挡,如果添加5张以上的卡,它们将不可见(屏幕外)。我正在尝试在KV中的GridLayout上添加一个scrollview。在找到一些话题之后,我发现了这种方式。
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,"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,"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,"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,"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
我该如何解决?
解决方法
该错误是由__init__()
类的Controller
方法引起的。此时,尚未分配layout_content
属性。您可以通过重新定义__init__()
来消除该Controller
方法:
class Controller(FloatLayout):
pass
然后通过修改ScrollView
的{{1}}部分来达到相同的预期结果:
kv
请注意 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__()
中的行强制设置得太小。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。