有谁愿意解释一下为什么来自Kivy ToggleButton的组名必须是组中一个按钮中的一个id的名称呢?
我遇到了一个问题,并努力解决这个问题,最终发现唯一的解决方案是使用其中一个按钮的id名称,否则解释器会说组名未定义。我不明白为什么。我看过Kivy Docs,却找不到任何关于团体本身的信息。它只说按钮可以使用组来创建单选按钮样式的活动。我使用Kivy 1.10.0和Python3.6.2
我在下面重新描述了这个问题。我非常感谢你对这件事的洞察力。
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.togglebutton import ToggleButton
class toggling(BoxLayout):
pass
class togglingApp(App):
pass
if __name__ == "__main__":
togglingApp().run()
kv文件:
toggling:
<toggling>:
orientation: "vertical"
padding: 5
spacing: 5
BoxLayout:
size_hint_y: None
height: "40dp"
Label:
text: "Title"
BoxLayout:
size_hint_y: None
height: "40dp"
Label:
size_hint_x: None
width: "100dp"
text: "Firstly"
ToggleButton:
text:"A1"
group: a1
id: a1
on_press:
ToggleButton:
text:"A2"
group: a1
id: faulty
on_press:
BoxLayout:
size_hint_y: None
height: "40dp"
Label:
size_hint_x: None
width: "100dp"
text: "Secondly"
ToggleButton:
text:"B1"
id: b1
group: the_b_group
on_press:
ToggleButton:
text:"B2"
id: b2
group: the_b_group
on_press:
ToggleButton:
text:"B3"
id: b3
group: the_b_group
on_press:
BoxLayout:
背景(如有需要):
我的问题源于一个错误,说明我的ToggleButton组名称没有定义。我有一些按预期工作的ToggleButtons,然后尝试将类似的一组按钮复制并粘贴到新行,并编辑标签。这个动作扼杀了应用程序。它不会运行,并且它说没有定义我的组名(用于新的一组按钮)。
我试图创建一个数据收集应用程序,这是一系列的类别,每个系列有2-5个按钮。这个想法是点击每个组中的一个按钮,每个按钮中的数据填充一个listview,并最终在数据库中进行文件处理。
诚然,我的Python技能很弱,我只做了几个月,但是我的技能比较弱。Kivy真的很棒,但是不管出于什么原因,我发现Kivy是完全不透明的,我通常不明白它在做什么。谢谢你的帮助。
发布于 2017-10-17 09:57:46
该按钮的group
的名称不需要来自于一个按钮的id
。group
名称是一个字符串。详情请参阅下面的例子。
切换按钮也可以分组以生成单选按钮--一个组中只有一个按钮可以处于“向下”状态。组名可以是字符串或任何其他可哈斯的Python对象:
警告 当将值赋值给id时,请记住该值不是字符串。没有引号:好的-> id: value,坏的-> id:'value‘
示例
main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class toggling(BoxLayout):
pass
class togglingApp(App):
def build(self):
return toggling()
if __name__ == "__main__":
togglingApp().run()
toggling.kv
#:kivy 1.10.0
<toggling>:
orientation: "vertical"
padding: 5
spacing: 5
BoxLayout:
size_hint_y: None
height: "40dp"
Label:
text: "Title"
BoxLayout:
size_hint_y: None
height: "40dp"
Label:
size_hint_x: None
width: "100dp"
text: "Firstly"
ToggleButton:
text:"A1"
group: "a_group"
id: a1
on_press:
ToggleButton:
text:"A2"
group: "a_group"
id: faulty
on_press:
BoxLayout:
size_hint_y: None
height: "40dp"
Label:
size_hint_x: None
width: "100dp"
text: "Secondly"
ToggleButton:
text:"B1"
id: b1
group: "the_b_group"
on_press:
ToggleButton:
text:"B2"
id: b2
group: "the_b_group"
on_press:
ToggleButton:
text:"B3"
id: b3
group: "the_b_group"
on_press:
BoxLayout:
输出
https://stackoverflow.com/questions/46794993
复制相似问题