如何在KivyMD的MDFloatingActionButtonSpeedDial中更改锚点值?

时间:2020-07-16 13:49:40

标签: python button kivy-language

我一直在与kivymd合作开发移动应用程序。但是我在MDFloatingActionButtonSpeedDial上遇到问题。我想将MDFloatingActionButtonSpeedDial中的锚点值设置为某些按钮“ ”,并将某些按钮设置为“ ”。因此,我修改了模块“ kivymd.uix.button ”中的 MDFloatingActionButtonSpeedDial 类,并在“ anchor ”中添加了更多选项假定为OptionProperty。同时,我还添加了应用更改所需的方法。 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)

我添加了left anchor属性,因为默认情况下显示了right anchor。

我编写的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'

现在,每当我尝试将构建器字符串文件中的锚属性设置为默认选项(右)以外的“左”时,程序都会返回错误。我想通知是否将锚的默认值设置为left,然后将锚设置为右的错误,当将锚的默认值设置为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文件中未设置任何锚点值时,我得到的输出(这是不希望的)是: I only get the floating button in the right. But the output that I desire is I wanted two floating buttons to be present on both side of the screen i.e. left as well as right. But I am getting only on one side and so i started to change the module but ended up with an error.

因此,如果您能为我提供解决此错误的解决方案,以便同时使用锚属性“ left”和“ right”,我将感到很高兴。

0 个答案:

没有答案