我最近发布了有关在屏幕上添加进度条窗口小部件的信息,但是由于我将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。帮助将不胜感激!
答案 0 :(得分:0)
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()