KivyMD 试图将小部件(进度条)的实例添加到屏幕

KivyMD trying to add instances of a widget (progressbar) to a Screen

.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 Workout(MDApp):
    dialog = 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()
    HomeScreen().add_widget(progressbar)

.kv 文件

MDToolbar:
    title: 'App Attack'
    type: 'top'
    right_action_items: [['plus', lambda x: app.AddNewGoal_Dialog()]]
    elevation: 10

因此,当我按下工具栏上的正确操作项时,它会创建对话框。然后,我想要它,所以当我按下对话框上的 CREATE Raised Button 时,它会向主屏幕添加一个小部件(进度条)。我没有收到 错误代码 。相反,当按钮被释放时,没有任何反应。有人可以帮忙吗?

您的 addNewGoal() 方法中的行:

HomeScreen().add_widget(progressbar)

创建 HomeScreen 的新实例,并将 progressbar 添加到该实例。不幸的是,HomeScreen 的实例不是您的 GUI 中的实例。您需要获取对 GUI 中 HomeScreen 实例的引用。如果您发布的 kv 文件是准确的,那么就没有 HomeScreen 的实例。但是,我怀疑您没有提供完整的 kv 文件。

如果您的 addNewGoal() 方法是您的 App 的方法(从您发布的代码中也不清楚),那么 addNewGoal() 中的那一行应该是这样的:

self.root.get_screen('homescreen').add_widget(progressbar)

假设您的 GUI 的根是 ScreenManager 并且 HomeScreennamehomescreen