0

我最近发布了关于向屏幕添加进度条小部件的帖子,但是由于我的 MDBottomNavigation 全部包含在一个屏幕中,因此每次切换到底部导航上的不同项目时,进度条都会保持存在。所以,我想要做的是将进度条添加到MDBottomNavigation 项目之一,而不是其他 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)

是需要进行编辑的地方。我知道这条线是不对的,我只是不确定如何调用我给出的 MDBottomNavigationItem id: 'GoalsBN'。帮助将不胜感激!

4

1 回答 1

0

问题

  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(progressbar)

片段

.kv 文件

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

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

主文件

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

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

输出

在此处输入图像描述

于 2020-08-05T02:56:58.593 回答