【问题标题】:How to work with KivyMD Checkboxes in Python如何在 Python 中使用 KivyMD 复选框
【发布时间】:2021-07-18 09:37:43
【问题描述】:

我用checkboxes 制作了MDDialog。当玩家检查特定的“分数限制”时,我想回调特定的alertdialog(MDDialog)。游戏是用python 3.9.6 kivymd 0.104.2 制作的。这里没有问题。我只是不知道该怎么做。这是我的python 代码:

kv = '''
#:import Factory kivy.factory.Factory
<ItemConfirm>
    divider: None
    CheckboxLeftWidget:
        id: check
        score: root.text  # makes the score text available from the Checkbox
        group: "check"
        on_active: app.check_active(self.group)  # update app.active

<ConfirmButton@MDFlatButton>:
    text: 'CONFIRM'
    disabled: not app.active  #  disabled when app.active is False
    on_release: app.score_limit()

<ConfirmDialog>:
    title: "Score Limit"
    bg_color: (5, 0, 0)
    type: "confirmation"
    auto_dismiss: False
    items:
        [Factory.ItemConfirm(text="30"),
        Factory.ItemConfirm(text="40"),
        Factory.ItemConfirm(text="50")]
    buttons: [ Factory.ConfirmButton() ]
'''

class ItemConfirm(OneLineAvatarIconListItem):
    pass


class ConfirmDialog(MDDialog):
    pass

class Begindup(MDFloatLayout):
    aa = 0

    def close_winner(self, *args):
        self.victory.dismiss()

    def __int__(self, numb):
        self.aa = numb

    def screen2(self, screenId):
        if self.pressed:
            self.ids.screenId.md_bg_color = 0, 0, 0, 1
            self.pressed = not self.pressed
        else:
            self.ids.screenId.md_bg_color = 1, 1, 1, 1
            self.pressed = not self.pressed

    def update_turn2(self, teamtwo_turn):
        self.ids.teamtwo_turn.text = (f"{teamtwo_turn}'s Turn")

    def update_score(self, score_one, *args):
        self.ids.score_one.text = score_one
        winner = int(self.ids.score_one.text)
        if (winner >= 30 and winner <= 39 and self.aa == 0):
            self.aa = 1
            show_winner(self)

        if (winner >= 40 and winner <= 49 and self.aa == 0):
            self.aa = 1
            show_winnerdup(self)

        if (winner >= 50 and winner <= 59 and self.aa == 0):
            self.aa = 1
            show_winnerdupdup(self)

        if (winner >= 60 and winner <= 80 and self.aa == 0):
            self.aa = 1
            show_winnerf(self)

class MyApp(MDApp):
    active = BooleanProperty(False)  # keeps track if any Checkbox is active

    def show_confirmation_dialog(self, *args):
        self.dialog = ConfirmDialog()
        self.dialog.open()

    def score_limit(self, *args, **kwargs):
        self.dialog.dismiss()
        self.dialog = None  # required to eliminate current group of Checkboxes
        self.active = False

    def check_active(self, group):  # update app.active and return current score limit (or None)
        for cb in MDCheckbox.get_widgets(group):
            if cb.active:
                self.active = True
                return cb.score
        self.active = False
        return None

    def build(self):
        Builder.load_string(kv)

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    美好的一天。您可以创建函数alertDialog 作为应用方法。

    如果您在访问小部件时遇到问题,请使用ids

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-08
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 2012-10-25
      • 2021-09-16
      • 1970-01-01
      相关资源
      最近更新 更多