打印哪个 Radio-ToggleButton 处于按下状态

我对 Python 和 Kivy 都是新手。我们正在为一项作业开发此应用程序,我正在处理首选项页面。我的标签、文本输入、切换按钮和按钮都位于正确的位置并且可以正常工作,但我正在努力解决如何获取这些按钮的输出。


基本上,当有人按下确认按钮时,我想打印这样的输出:


Name: Blabla

Mood reminders: 2 per day

Diary reminders: 1 per day

我认为 on_press 不起作用,因为每次按下按钮时都会打印一个值,而不是仅在按下确认时打印一个值。


现在我的 .py 代码如下所示


from kivy.app import App

from kivy.lang import Builder

from kivy.uix.floatlayout import FloatLayout

from kivy.properties import ObjectProperty


# Builder.load_file('preferences.kv')


class MyFloatLayout(FloatLayout):

    name = ObjectProperty(None)

    def mood_buttons(self):

        pass

    def diary_buttons(self):

        pass


    def print_specs(self):

        print("Name: " + self.name.text



class PreferencesApp(App):

    def build(self):

        return MyFloatLayout()


if __name__ == '__main__':

    PreferencesApp().run()

我的 .kv 语言文件如下所示


<Label>

    font_size: 20

<TextInput>

    font_size: 20

<Checkbox>

    size_hint_x: .20

<MyFloatLayout>:

    name: name

    FloatLayout:

        Label:

            text: "Name: "

            size_hint: 0.4, 0.08

            pos_hint:{"x": 0.1, "center_y": 0.85}

        TextInput:

            id: name

            multiline: False

            size_hint: 0.4, 0.07

            pos_hint:{"x":0.5,"center_y":0.85}


        Label:

            text: "How many mood reminders would you like?"

            size_hint: 0.5, 0.1

            pos_hint: {"center_x": 0.5, "top": 0.75}

        BoxLayout:

            pos_hint: {'center_x': 0.5, 'y': 0.55}

            size_hint: 0.8, 0.1

            ToggleButton:

                text:"Zero"

                id: m0

                group: "mood_popups"

                on_press: self.mood_buttons()

            ToggleButton:

                text:"1 per day"

                id: m1

                group: "mood_popups"

            ToggleButton:

                text:"2 per day"

                id: m2

                group: "mood_popups"

            ToggleButton:

                text:"3 per day"

                id: m3

                group: "mood_popups"



HUWWW
浏览 82回答 1
1回答

哈士奇WWW

Buttons您可以使用该方法访问组中的所有内容get_widgets()。所以你的print_specs()方法可以是:def print_specs(self):&nbsp; &nbsp; print("Name: " + self.name.text)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; # get mood reminder&nbsp; &nbsp; mood_reminder = ''&nbsp; &nbsp; for toggle in ToggleButton.get_widgets('mood_popups'):&nbsp; &nbsp; &nbsp; &nbsp; if toggle.state == 'down':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mood_reminder = toggle.text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; print('Mood reminders:', mood_reminder)&nbsp; &nbsp; # get diary reminder&nbsp; &nbsp; diary_reminder = ''&nbsp; &nbsp; for toggle in ToggleButton.get_widgets('diary_popups'):&nbsp; &nbsp; &nbsp; &nbsp; if toggle.state == 'down':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diary_reminder = toggle.text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; print('Diary reminders:', diary_reminder)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python