我一直在与 kivymd 合作开发移动应用程序。但我遇到了有关 MDFloatingActionButtonSpeedDial 的问题。我想将 MDFloatingActionButtonSpeedDial 中的锚点值设置为“left”(对于某些按钮)和“right”(对于某些按钮)。因此,我修改了模块“kivymd.uix.button”中的MDFloatingActionButtonSpeedDial类,并在假定OptionProperty的“anchor”中添加了更多选项。除此之外,我还添加了应用更改所需的方法。 acnhor 的变化是:-
anchor = OptionProperty('right',option=["right","left"])
方法的变化是:-
def set_pos_labels(self, widget):
"""Sets the position of the floating labels."""
if self.anchor == "right":
widget.x = Window.width - widget.width - dp(86)
elif self.anchor == "left":
widget.x = widget.width + dp(86)
def set_pos_root_button(self, instance):
"""Sets the position of the root button."""
if self.anchor == "right":
instance.y = dp(20)
instance.x = Window.width - (dp(56) + dp(20))
elif self.anchor == "left":
instance.y = dp(20)
instance.x = (dp(56) + dp(20))
def set_pos_bottom_buttons(self, instance):
"""Sets the position of the bottom buttons in a stack."""
if self.anchor == "right":
if self.state != "open":
instance.y = instance.height / 2
instance.x = Window.width - (instance.height + instance.width / 2)
elif self.anchor == "left":
if self.state != "open":
instance.y = instance.height / 2
instance.x = (instance.height + instance.width / 2)
我添加了左锚点属性,因为默认情况下存在右锚点。
我写的Builder字符串如下:-
MDFloatingActionButtonSpeedDial:
data : app.data_download
rotation_root_button : True
id : download_button
icon: 'download'
anchor: 'left'
MDFloatingActionButtonSpeedDial:
data : app.data_return
rotation_root_button: True
icon : 'arrow-left'
anchor: 'right'
现在,每当我尝试将锚点属性设置为“左”而不是构建器字符串文件中的默认选项(右)时,程序都会返回错误。我想告知,如果我将锚点的默认值设置为左侧,则会收到将锚点设置为右侧的错误,而当我将锚点的默认值设置为右侧时,则会收到将锚点设置为左侧的错误。我收到的错误是:-
Traceback (most recent call last):
File "C:\Users\user\Anaconda3\lib\site-packages\kivy\lang\builder.py", line 700, in _apply_rule
setattr(widget_set, key, value)
File "kivy\weakproxy.pyx", line 35, in kivy.weakproxy.WeakProxy.__setattr__
File "kivy\properties.pyx", line 497, in kivy.properties.Property.__set__
File "kivy\properties.pyx", line 541, in kivy.properties.Property.set
File "kivy\properties.pyx", line 532, in kivy.properties.Property.set
File "kivy\properties.pyx", line 1252, in kivy.properties.OptionProperty.check
ValueError: MDFloatingActionButtonSpeedDial.anchor is set to an invalid option 'left'. Must be one
of: []
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "onlinecv2.py", line 500, in <module>
ScanIt().run()
File "C:\Users\user\Anaconda3\lib\site-packages\kivy\app.py", line 829, in run
root = self.build()
File "onlinecv2.py", line 435, in build
camerascreen = Builder.load_string(screen_helper)
File "C:\Users\user\Anaconda3\lib\site-packages\kivy\lang\builder.py", line 405, in load_string
rule_children=rule_children)
File "C:\Users\user\Anaconda3\lib\site-packages\kivy\lang\builder.py", line 707, in _apply_rule
e), cause=tb)
kivy.lang.builder.BuilderException: Parser: File "<inline>", line 154:
...
152: id : download_button
153: icon: 'download'
>> 154: anchor: 'left'
155:
156:
...
ValueError: MDFloatingActionButtonSpeedDial.anchor is set to an invalid option 'left'. Must be one
of: []
File "C:\Users\user\Anaconda3\lib\site-packages\kivy\lang\builder.py", line 700, in _apply_rule
setattr(widget_set, key, value)
File "kivy\weakproxy.pyx", line 35, in kivy.weakproxy.WeakProxy.__setattr__
File "kivy\properties.pyx", line 497, in kivy.properties.Property.__set__
File "kivy\properties.pyx", line 541, in kivy.properties.Property.set
File "kivy\properties.pyx", line 532, in kivy.properties.Property.set
File "kivy\properties.pyx", line 1252, in kivy.properties.OptionProperty.check
当我没有在 kivy 文件中设置任何锚点值时,我得到的输出(这是不需要的)如下:-
因此,如果您能为我提供一个解决方案来解决此错误,以便我可以同时使用锚点属性“左”和“右”,我将很高兴。
当您在 OptionProperty 中使用多个选项时,应该使用
options
而不是 option
anchor = OptionProperty("right, options=("left", "right"))