如何在 KivyMD 中使用提升来提升圆形图标按钮?

问题描述 投票:0回答:1

我似乎无法在我的 KivyMD 项目中完成这项工作。我已多次阅读该文档,但我不知道如何按照我想要的方式获得它。这是我的Python代码:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.behaviors import ButtonBehavior

from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.screen import MDScreen
from kivy.core.window import Window

from kivymd.uix.behaviors import CommonElevationBehavior, CircularRippleBehavior
from kivymd.uix.floatlayout import MDFloatLayout
Window.size = (375, 812)

class LoginWindow(MDScreen):
    pass

class MainWindow(MDScreen):
    pass

class PlayerWindow(MDScreen):
    pass

class WindowManager(ScreenManager):
    pass

class CircularElevationButton(CommonElevationBehavior, CircularRippleBehavior, 

ButtonBehavior, MDFloatLayout):
        pass

class myMusicApp(MDApp):
    def build(self):
        kv = Builder.load_file('myMusicApp.kv')
        return kv
    
if __name__ == '__main__':
    myMusicApp().run()

这是我的 .kv 文件:

WindowManager:
    LoginWindow:
    MainWindow:
    PlayerWindow:

<LoginWindow>:
    name: 'login'
    Button:
        text: 'Log in'
        on_release: app.root.current = 'player'

<MainWindow>:
    name: 'main'

<CircularElevationButton>
    size_hint: None, None
    size: "100dp", "100dp"
    radius: self.size[0] / 2
    shadow_radius: self.radius[0]
    md_bg_color: "red"

    MDIcon:
        icon: "hand-heart"
        halign: "center"
        valign: "center"
        pos_hint: {"center_x": .5, "center_y": .5}
        size: root.size
        pos: root.pos
        font_size: root.size[0] * .6
        theme_text_color: "Custom"
        text_color: "white"

<PlayerWindow>:
    name: 'player'
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size
    BoxLayout:
        orientation: 'vertical'
        BoxLayout
            size_hint: 1, .06
            orientation: 'horizontal'
            MDIconButton:
                anchor_x: 'left'
                anchor_y: 'middle'
                icon: 'arrow-left'
                style: 'standard'
            MDLabel:
                text: 'Now Playing'
                halign: 'center'
                font_name: 'Roboto'
            MDIconButton:
                icon: 'menu'
        Image:
            id: 'playerImage'
            source: '/Users/name/Desktop/codingProjects/kivy/myNewApp3/ab67616d0000b273715973050587fe3c93033aad.jpeg'
        BoxLayout:
            size_hint: 1, .15
            orientation: 'vertical'
            spacing: '10dp'
            padding: '10dp'
            BoxLayout:
                orientation: 'vertical'
                CircularElevationButton:
                    pos_hint: {"center_x": .5, "center_y": .6}
                    elevation: 4
                    shadow_softness: 4
                

根据文档,它应该是这样的:

enter image description here

这就是我的结果。没有可见的高度,并且图标大小 (font_size) 无法更改。更改海拔数字也没有任何作用。

enter image description here

python kivy kivymd
1个回答
0
投票

我相信您需要使用

ButtonElevationBehaviour
而不是
CommonElevationBehavior
。尝试更换:

class CircularElevationButton(CommonElevationBehavior, CircularRippleBehavior, ButtonBehavior, MDFloatLayout):
        pass

与:

class CircularElevationButton(ButtonElevationBehaviour, CircularRippleBehavior, MDIconButton):
    pass

添加

MDIconButton
简化了图标的使用。

© www.soinside.com 2019 - 2024. All rights reserved.