【问题标题】:KivyMD adding Widget to a single MDBottomNavigation item/screenKivyMD 将小部件添加到单个 MDBottomNavigation 项目/屏幕
【发布时间】:2020-07-30 14:47:40
【问题描述】:

我最近发布了关于向屏幕添加进度条小部件的帖子,但由于我将 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'。非常感谢您的帮助!

【问题讨论】:

  • 在您的 kv 文件中,将 id: 'GoalsBN' 替换为 id:GoalsBN
  • 现在我得到错误代码:AttributeError: 'super' object has no attribute 'getattr'
  • 请用更改和完整的追溯错误更新您的帖子。

标签: python kivy kivymd


【解决方案1】:

问题

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

输出

猜你喜欢
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 2021-05-11
  • 2020-08-24
  • 2014-08-06
  • 2022-08-21
相关资源
最近更新 更多