基维数号。的切换按钮在按下"Counter"按钮时处于打开状态



在这段代码中,我有一堆座位(ToggleButtons),我想计算一下编号。当按下按钮"Counter"时被选中或打开的切换按钮。我不确定是否,为了获得结果,我必须从所有ToggleButtons(座椅)单独调用一个函数,或者在主按钮"Counter"中调用一个函数。如果可能的话,我也需要打印他们的id。

主要代码:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import Screen,ScreenManager

class Main(Screen):
def counter(self,widget):
pass # what to put here? or do u have any other method
class Manager(ScreenManager):
pass
kv=Builder.load_file("test2.kv")
screen=Manager()
screen.add_widget(Main(name="main"))
class Test(App):
def build(self):
return screen
Test().run()

Kv代码:

<Main>:
name: "main"
FloatLayout:
id: Fl2
Button:
id: counter1
text: "Counter"
size_hint: (.25,.1)
pos_hint: {"center_x":.5,"center_y":.3}
on_press:
root.counter(self) # this should count no. of buttons that are ON i.e. the selected buttons
ToggleButton:
id: Seat_A2
color: (0,0,0,0)
text: "Seat_A2"
size_hint: (.0225,.0120)
pos_hint: {"center_x":.4,"center_y":.75}
background_color: (1,1,1,1)
on_state:
root.count(self) #i'm not sure if this func needs to be called individually 
on all the toggle buttons or just one func in the counter button (i.e. root.counter(self))... idk the method... 
ToggleButton:
id: Seat_A3
color: (0,0,0,0)
text: "Seat_A3"
size_hint: (.0225,.0120)
pos_hint: {"center_x":.4225,"center_y":.75}
background_color: (1,1,1,1)
on_state:
root.count(self)
ToggleButton:
id: Seat_A6
color: (0,0,0,0)
text: "Seat_A6"
size_hint: (.0225,.0120)
pos_hint: {"center_x":.4900,"center_y":.75}
background_color: (1,1,1,1)
on_state:
root.count(self)
ToggleButton:
id: Seat_A7
color: (0,0,0,0)
text: "Seat_A7"
size_hint: (.0225,.0120)
pos_hint: {"center_x":.5120,"center_y":.75}
background_color: (1,1,1,1)
on_state:
root.count(self)

ToggleButton:
id: Seat_A10
color: (0,0,0,0)
text: "Seat_A10"
size_hint: (.0225,.0120)
pos_hint: {"center_x":.5800,"center_y":.75}
background_color: (1,1,1,1)
on_state:
root.count(self)
ToggleButton:
id: Seat_A11
color: (0,0,0,0)
text: "Seat_A11"
size_hint: (.0225,.0120)
pos_hint: {"center_x":.6025,"center_y":.75}
background_color: (1,1,1,1)
on_state:
root.count(self)

您可以处理包含ToggleButtonsFloatLayout的子节点,如下所示:

def counter(self,widget):
toggles = []
for child in self.ids.Fl2.children:
if isinstance(child, ToggleButton):
if child.state == 'down':
toggles.append(child.text)
print(len(toggles), 'ToggleeButtons active:', toggles)

最新更新