0

.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 时,它将向主屏幕添加一个小部件(进度条)。我没有收到错误代码。相反,当按钮被释放时,什么也没有发生。任何人都可以帮忙吗?

4

1 回答 1

0

您的addNewGoal()方法中的行:

HomeScreen().add_widget(progressbar)

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

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

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

假设您的 GUI 的根是 aScreenManager并且name您的HomeScreenhomescreen

于 2020-07-27T15:21:22.673 回答