KivyMD 将 Widget 添加到单个 MDBottomNavigation item/screen

KivyMD adding Widget to a single MDBottomNavigation item/screen

我最近发布了关于向屏幕添加进度条小部件的帖子,但由于我的 MDBottomNavigation 包含在一个屏幕中,每次我切换到底部导航中的不同项目时,进度条都会保持存在。所以,我想要做的是将进度条添加到 MDBottomNavigation 项目的 one 而不是其他 2。这是我的代码:

.py 文件

class WindowManager(ScreenManager):
    pass 

class HomeScreen(Screen):
   pass

class MyWorkouts(Screen):
    pass

class RecommendedWorkouts(Screen):
    pass

class AddWorkouts(Screen):
    pass

class CreateNewWorkout(Screen):
    pass

class AddNewGoal(Screen):
    pass

class Goals(Screen):
    pass

class Workout(MDApp):
    dialog = None
    PB = ObjectProperty(None)

def build(self):
    return 

def AddNewGoal_Dialog(self):
    if not self.dialog:
        self.dialog = MDDialog(
            size_hint_x = 0.8,
            size_hint_y = 1,
            pos_hint = {'center_x': .5, 'center_y': .5},
            radius = [10, 10, 10, 10],
            title = 'Add New Goal',
            auto_dismiss = False,
            type = 'custom',
            content_cls = AddNewGoal(),
            buttons = [
                MDFlatButton(
                    text = 'CANCEL', text_color = self.theme_cls.primary_color, 
                    on_release = self.closeDialog),
                MDRaisedButton(
                    text = 'CREATE', text_color = self.theme_cls.primary_color,
                    on_release = self.addNewGoal)
                    
                ],
        )
    self.dialog.open()

def addNewGoal(self, inst):
    progressbar = ProgressBar(
    value = 50,
    max = 100
    )

    self.root.ids.GoalsBN.add_widget(progressbar)
    self.dialog.dismiss()

.kv 文件

WindowManager:
    transition: FadeTransition(duration = 1)
HomeScreen:

MyWorkouts:

RecommendedWorkouts:

AddWorkouts:

CreateNewWorkout:

AddNewGoal:

Goals:


<HomeScreen>
    name: 'HomeScreen'


MDBottomNavigation:
    MDBottomNavigationItem:
        text: 'Workouts'
        name: 'Workouts'
        icon: 'weight-lifter'

        FloatLayout:
            canvas:
                Color:
                    #rgba: 0.2, 0.4, 0.75, 0.8
                    rgba: 0, 0, 0.5, 0.9
                Rectangle:
                    pos: self.pos
                    size: self.size


            MDFillRoundFlatIconButton:
                text: 'My Workouts'
                text_color: 0, 0, 0, 1
                font_size: '12sp'
                icon: 'weight-lifter'
                pos_hint: {'x': 0.15, 'top': 0.7}
                size_hint: 0.7, 0.1

        
                on_release:
                    app.root.current  = 'MyWorkouts' 


            MDFillRoundFlatIconButton:
                text: 'Recommended Workouts'
                text_color: 0, 0, 0, 1
                font_size: '12sp'
                icon: 'weight-kilogram'
                pos_hint: {'x': 0.15, 'top': 0.5}
                size_hint: 0.7, 0.1
                background_normal: 'white.jpg'

                on_release:
                    app.root.current = 'RecommendedWorkouts'
            
        GridLayout:
            cols: 1

            MDToolbar:
                title: 'App Attack'
                type: 'top'
                #md_bg_color: app.theme_cls.accent_color
                elevation: 10

    
    MDBottomNavigationItem:
        text: 'Goals'
        id: 'GoalsBN'
        name: 'GoalsBN'
        icon: 'trending-up'


        FloatLayout:
            canvas:
                Color:
                    #rgba: 0.2, 0.4, 0.75, 0.8
                    rgba: 0, 0, 0.5, 0.9
                Rectangle:
                    pos: self.pos
                    size: self.size
                    

            GridLayout:
                cols: 1

                MDToolbar:
                    title: 'App Attack'
                    type: 'top'
                    #md_bg_color: app.theme_cls.accent_color
                    elevation: 10
                    right_action_items: [['plus-circle-outline', lambda x: app.AddNewGoal_Dialog()]]



    MDBottomNavigationItem:
        text: 'Profile'
        name: 'Profile'
        icon: 'account'

        FloatLayout:
            canvas:
                Color:
                    rgba: 0, 0, 0.5, 0.9
                Rectangle:
                    pos: self.pos
                    size: self.size

        GridLayout:
            cols: 1

            MDToolbar:
                title: 'App Attack'
                type: 'top'
                #md_bg_color: app.theme_cls.accent_color
                elevation: 10 

.py文件中的行

self.root.ids.GoalsBN.add_widget(progressbar)

是需要编辑的地方。我知道这条线不对,我只是不确定如何调用我给定 ID 的 MDBottomNavigationItem:'GoalsBN'。非常感谢您的帮助!

问题

  1. 在kv文件中,id:'GoalsBN'定义为字符串。
  2. id: 'GoalsBN' 在 HomeScreen 中定义但在 ScreenManager 中未定义,WindowManager:.

解决方案

  1. 在kv文件中,id:不是字符串。因此,将 id: 'GoalsBN' 替换为 id: GoalsBN
  2. 在 kv 文件中,添加 id: homeScreen 并在 Python 脚本中,将 ids.homeScreen 添加到 self.root.ids.GoalsBN.add_widget(进度条)

片段

kv 文件

WindowManager:
    transition: FadeTransition(duration = 1)
    HomeScreen:
        id: homeScreen

    MyWorkouts:
    ...
        MDBottomNavigationItem:
            text: 'Goals'
            id: GoalsBN
            ...

main.py

    def addNewGoal(self, inst):
        progressbar = ProgressBar(
            value=50,
            max=100
        )

        self.root.ids.homeScreen.ids.GoalsBN.add_widget(progressbar)
        self.dialog.dismiss()

输出