Python 如何在弹出窗口中保存我的复选框的值
我有一个按钮,当点击时,会出现一个弹出窗口,在这个弹出窗口上,有一个带有复选框的列表。当我单击复选框时,它们会响应并变为活动或非活动状态。问题是当我关闭并重新打开弹出窗口时,滴答声消失了。即使在弹出窗口关闭后,如何使某些复选框处于活动状态 我曾尝试使用id,其中每个复选框都有一个id,如果选中某个复选框,我会手动执行某些操作,但失败了,因为我无法获取该复选框的状态Python 如何在弹出窗口中保存我的复选框的值,python,kivy,Python,Kivy,我有一个按钮,当点击时,会出现一个弹出窗口,在这个弹出窗口上,有一个带有复选框的列表。当我单击复选框时,它们会响应并变为活动或非活动状态。问题是当我关闭并重新打开弹出窗口时,滴答声消失了。即使在弹出窗口关闭后,如何使某些复选框处于活动状态 我曾尝试使用id,其中每个复选框都有一个id,如果选中某个复选框,我会手动执行某些操作,但失败了,因为我无法获取该复选框的状态 class SwipeDhikrScreen(Screen): counter = NumericProperty(0)
class SwipeDhikrScreen(Screen):
counter = NumericProperty(0)
counter_text = StringProperty(str(counter)) #
def on_touch_move(self, touch):
if touch.x - touch.ox > 50:
MainApp.get_running_app().change_screen(screen_name="home_screen", screen_direction="right")
def dhikr_count(self):
self.counter += 1
print(self.counter)
self.counter_text = str(self.counter)
def custompop_up(self): # calls open the pop up custom window
content = CustomOptionPopUp()
global Cpop
Cpop = Popup(title="Custom Dhikr", content=content, size_hint=(None, None), size=(250, 400))
Cpop.open()
class CustomOptionPopUp(FloatLayout):
subhanAllah_click = ObjectProperty(None)
alhamdullilah_check = ObjectProperty(None)
def checkbox_click(self, instance, value): # Deals with the okay button of the Custom pop up window
if value:
print(self.ids)
print(self.subhanAllah_click)
def quit_custom_popup(self):
Cpop.dismiss() # closes the custom pop up window
这是kv文件
<SwipeDhikrScreen>
FloatLayout:
GridLayout:
rows: 1
pos_hint: {"left": 0.3, "y": 0.7}
Label:
text_size: self.size
size: self.texture_size
text: "SubhanAllah"
font_size: 20
halign: "center"
Button: # this will be an image button
id: counter
text: "Press Me"
pos_hint: {"x":0.35, "y":0.5}
size_hint: 0.3, 0.1
halign: "center"
on_release: root.dhikr_count()
Label:
#id: count_num
text: root.counter_text
pos_hint: {"x":0.35, "y":0.43}
size_hint: 0.3, 0.1
halign: "center"
Button:
text: "Custom"
pos_hint: {"x": 0.38, "y":0.35}
size_hint: 0.25, 0.07
halign: "center"
on_release: root.custompop_up()
<CustomOptionPopUp>
checkbox: subhanAllah_check
Label:
text: "SubhanAllah"
font_size: 20
pos_hint: {"x": 0.1, "y": 0.8}
size_hint: 0.3, 0.1
halign: "center"
CheckBox:
id: subhanAllah_check
pos_hint: {"x": 0.6, "y": 0.8}
size_hint: 0.3, 0.1
on_active: root.checkbox_click(self, self.active)
Label:
text: "Alhamdullilah"
font_size: 20
pos_hint: {"x": 0.1, "y": 0.7}
size_hint: 0.3, 0.1
halign: "center"
CheckBox:
id: alhamdullilah_check
pos_hint: {"x": 0.6, "y": 0.7}
size_hint: 0.3, 0.1
on_active: root.checkbox_click(self, self.active)
Label:
text: "AllahuAkbar"
font_size: 20
pos_hint: {"x": 0.1, "y": 0.6}
size_hint: 0.3, 0.1
halign: "center"
CheckBox:
pos_hint: {"x": 0.6, "y": 0.6}
size_hint: 0.3, 0.1
on_active: root.checkbox_click(self, self.active)
Label:
text: "Lailahaillallah"
font_size: 20
pos_hint: {"x": 0.1, "y": 0.5}
size_hint: 0.3, 0.1
halign: "center"
CheckBox:
pos_hint: {"x": 0.6, "y": 0.5}
size_hint: 0.3, 0.1
on_active: root.checkbox_click(self, self.active)
Button: #OK button
text: "OK"
pos_hint: {"x":0.5, "y":0}
size_hint: 0.5, 0.1
on_release: root.quit_custom_popup()
浮动布局:
网格布局:
行数:1
pos_提示:{“left”:0.3,“y”:0.7}
标签:
文本大小:self.size
大小:self.texture\u大小
正文:“苏哈纳拉”
字体大小:20
哈利恩:“中心”
按钮:#这将是一个图像按钮
id:柜台
文字:“按我”
pos_提示:{“x”:0.35,“y”:0.5}
大小提示:0.3,0.1
哈利恩:“中心”
发布时:root.dhikr\u count()
标签:
#编号:count_num
text:root.counter\u text
pos_提示:{“x”:0.35,“y”:0.43}
大小提示:0.3,0.1
哈利恩:“中心”
按钮:
文本:“自定义”
pos_提示:{“x”:0.38,“y”:0.35}
尺寸提示:0.25,0.07
哈利恩:“中心”
发布时:root.custompop\u up()
复选框:Subhanalah_check
标签:
正文:“苏哈纳拉”
字体大小:20
pos_提示:{“x”:0.1,“y”:0.8}
大小提示:0.3,0.1
哈利恩:“中心”
复选框:
id:Subhanalah_检查
pos_提示:{“x”:0.6,“y”:0.8}
大小提示:0.3,0.1
启用时:root.checkbox\u单击(self,self.active)
标签:
文本:“阿罕杜利拉”
字体大小:20
pos_提示:{“x”:0.1,“y”:0.7}
大小提示:0.3,0.1
哈利恩:“中心”
复选框:
id:alhamdullilah_支票
pos_提示:{“x”:0.6,“y”:0.7}
大小提示:0.3,0.1
启用时:root.checkbox\u单击(self,self.active)
标签:
文本:“阿拉瓦克巴”
字体大小:20
pos_提示:{“x”:0.1,“y”:0.6}
大小提示:0.3,0.1
哈利恩:“中心”
复选框:
pos_提示:{“x”:0.6,“y”:0.6}
大小提示:0.3,0.1
启用时:root.checkbox\u单击(self,self.active)
标签:
文字:“Lailahaillallah”
字体大小:20
pos_提示:{“x”:0.1,“y”:0.5}
大小提示:0.3,0.1
哈利恩:“中心”
复选框:
pos_提示:{“x”:0.6,“y”:0.5}
大小提示:0.3,0.1
启用时:root.checkbox\u单击(self,self.active)
按钮:#确定按钮
文本:“OK”
pos_提示:{“x”:0.5,“y”:0}
尺寸提示:0.5,0.1
发布时:root.quit\u custom\u popup()
您的复选框值被重置的原因是,每次调用自定义弹出()时,您都要创建一个新的弹出窗口
(以及新的复选框
)。要更正此问题,只需创建一个弹出窗口
,以便在调用custompop\u up()
时显示。大概是这样的:
def custompop_up(self): # calls open the pop up custom window
global Cpop
if Cpop is None:
content = CustomOptionPopUp()
Cpop = Popup(title="Custom Dhikr", content=content, size_hint=(None, None), size=(250, 400))
Cpop.open()
这要求在调用custompop\u up()
之前定义Cpop
,因此以下语句应出现在任何类之外的代码中:
Cpop = None