KivyMD将小部件添加到单个MDBottomNavigation项/屏幕

时间:2020-07-30 14:47:40

标签: python kivy kivymd

我最近发布了有关在屏幕上添加进度条窗口小部件的信息,但是由于我将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)

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

1 个答案:

答案 0 :(得分: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
            ...

main.py

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

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

输出

enter image description here