我最近發布了關于向屏幕添加進度條小部件的信息,但由于我的 MDBottomNavigation 包含在一個屏幕中,每次我切換到底部導航中的不同項目時,進度條都會保持存在。所以,我想要做的是將進度條添加到MDBottomNavigation 項目中的一個而不是其他 2 個。這是我的代碼:.py文件class WindowManager(ScreenManager): pass class HomeScreen(Screen): passclass MyWorkouts(Screen): passclass RecommendedWorkouts(Screen): passclass AddWorkouts(Screen): passclass CreateNewWorkout(Screen): passclass AddNewGoal(Screen): passclass Goals(Screen): passclass 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()是需要進行編輯的地方。我知道這條線不對,我只是不確定如何調用我給定 id: 'GoalsBN'的 MDBottomNavigationItem 。幫助將不勝感激!
1 回答

慕森卡
TA貢獻1806條經驗 獲得超8個贊
問題
在 kv 文件中,id: 'GoalsBN'被定義為一個字符串。
id: 'GoalsBN'在 HomeScreen 中定義但未在 ScreenManager, WindowManager:中定義。
解決方案
在 kv 文件中,id:不是字符串。因此,將id: 'GoalsBN'替換為id: GoalsBN
在 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()
輸出
添加回答
舉報
0/150
提交
取消