我对 Python 和 Kivy 都是新手。我们正在为一项作业开发此应用程序,我正在处理首选项页面。我的标签、文本输入、切换按钮和按钮都位于正确的位置并且可以正常工作,但我正在努力解决如何获取这些按钮的输出。基本上,当有人按下确认按钮时,我想打印这样的输出:Name: BlablaMood reminders: 2 per dayDiary reminders: 1 per day我认为 on_press 不起作用,因为每次按下按钮时都会打印一个值,而不是仅在按下确认时打印一个值。现在我的 .py 代码如下所示from kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.floatlayout import FloatLayoutfrom 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.textclass 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"
1 回答
哈士奇WWW
TA贡献1799条经验 获得超6个赞
Buttons您可以使用该方法访问组中的所有内容get_widgets()。所以你的print_specs()方法可以是:
def print_specs(self):
print("Name: " + self.name.text)
# get mood reminder
mood_reminder = ''
for toggle in ToggleButton.get_widgets('mood_popups'):
if toggle.state == 'down':
mood_reminder = toggle.text
break
print('Mood reminders:', mood_reminder)
# get diary reminder
diary_reminder = ''
for toggle in ToggleButton.get_widgets('diary_popups'):
if toggle.state == 'down':
diary_reminder = toggle.text
break
print('Diary reminders:', diary_reminder)
添加回答
举报
0/150
提交
取消