Kivy:触摸标签以选中复选框 [英] Kivy: Touch the label to check the checkbox
问题描述
我正在努力为MCQ争取一个机会.在这里,我要一个功能,如果有人甚至单击与复选框相对应的标签,则也必须选中该复选框..是否可以在Kivy中进行?因为Kivy没有直接与复选框提供任何文本关联.
I am trying to make an aap for MCQ's. Here i want a function, if anyone even click on the label corresponding to a checkbox, that check box must also be checked.. Is it possible in Kivy? Because Kivy doesn't provide any text association with checkbox directly.
这是kv的一部分.
<MCQCheckBox@CheckBox>:
color:0,0,0,1
size_hint: 0.15, 1
group: 'opts'
<MCQLabel@Label>:
text_size: self.size
valign: 'center'
font_size: '13sp'
color: 0,0,0,1
<MCQsGUI>:
BoxLayout:
orientation:'vertical'
size_hint: 0.95, 0.7
spacing: 2
pos_hint: {'center_x': .5, 'center_y': .5}
MCQBoxLayout:
MCQCheckBox:
MCQLabel:
text:"option 1"
MCQBoxLayout:
MCQCheckBox:
MCQLabel:
text:"option 2"
MCQBoxLayout:
MCQCheckBox:
MCQLabel:
text:"option 3"
MCQBoxLayout:
MCQCheckBox:
MCQLabel:
text:"option 4"
推荐答案
您可以仅使用kv languaje和动态类来做到这一点:
You can do it using only kv languaje and dynamic classes:
-
要使标签的行为像按钮一样,只需使
MCQLabel
继承自Label
和ButtonBehavior
类.
要保持组自己的行为,您可以调用_do_press ()方法按下关联标签时,="=" nofollow noreferrer> ToggleButton
类(CheckBox继承自该类).
To keep the group's own behavior you can call the _do_press ()
method of the ToggleButton
class (CheckBox inherits from it) when the asociated label is pressed.
test.kv:
<MCQCheckBox@CheckBox>:
color: 0, 0, 0, 1
size_hint: 0.15, 1
<MCQLabel@ButtonBehavior+Label>:
text_size: self.size
valign: 'center'
font_size: '13sp'
color: 0, 0, 0, 1
<MCQLabelCheckBox@BoxLayout>:
text: ''
group: ''
MCQCheckBox:
id: cb
group: root.group
MCQLabel:
on_press: cb._do_press()
text: root.text
<MCQsGUI>:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
MCQLabelCheckBox:
text:"option 1"
group: 'opts'
MCQLabelCheckBox:
text:"option 2"
group: 'opts'
MCQLabelCheckBox:
text:"option 3"
group: 'opts'
MCQLabelCheckBox:
text:"option 4"
group: 'opts'
main.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class MCQsGUI(BoxLayout):
pass
class TestApp(App):
def build(self):
return MCQsGUI()
if __name__ == '__main__':
TestApp().run()
这篇关于Kivy:触摸标签以选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!