如何在 Kivy 中默认选中复选框?
Posted
技术标签:
【中文标题】如何在 Kivy 中默认选中复选框?【英文标题】:How do you make a checkbox checked by default in Kivy? 【发布时间】:2020-06-13 00:26:36 【问题描述】:我在 .KV 文件中创建了两个复选框(一个用于默认设置,一个用于自定义关键字),我试图在 GUI 最初打开此窗口时检查默认框。我能够正确设置默认值输出,但是,当窗口最初显示时,我无法让实际的复选框直观地显示它被选中。
该程序的目标是为我的实验室之前创建并完成的现有模拟软件制作基本的图形用户界面。
我对 Kivy 还很陌生,所以我不确定我是否做错了什么,或者我只是不明白复选框是如何正常工作的。
我已经尝试过在其他相关帖子中看到的两种方法
defaultCheckbox = BooleanProperty(True) #in .py file
我已经尝试添加
active: True # in the .kv file
这是 .kv 文件的复选框部分
画布部分是从另一个 *** 帖子复制的,其中在呈现页面时复选框后面的框没有显示,因此解决方案是手动绘制它。
FloatLayout:
BoxLayout:
orientation: "horizontal"
height: 20
pos_hint: 'center': (0.5, 0.7)
FloatLayout:
Label:
text: "Default \n Keywords"
size_hint_x: 1
halign: 'center'
pos_hint: 'center': (0.2, 0.5)
font_size: 30
#todo make default kwargs checkbox active on program boot
CheckBox:
id: defaultCheckbox
canvas.before:
Color:
rgb: 30,35,38
Ellipse:
pos:self.center_x-11, self.center_y-11
size:[22,22]
Color:
rgb: 0,0,0
Ellipse:
pos:self.center_x-10, self.center_y-10
size:[20,20]
on_active: root.default_click(self, self.active)
size_hint_x: .20
group: "keywords"
Label:
text: " "
valign: 'bottom'
FloatLayout:
Label:
text: "Define \n Experiment \n Keywords"
size_hint_x: 1
halign: 'center'
pos_hint: 'center': (0.15, 0.5)
font_size: 25
CheckBox:
canvas.before:
Color:
rgb: 30,35,38
Ellipse:
pos:self.center_x-11, self.center_y-11
size:[22,22]
Color:
rgb: 0,0,0
Ellipse:
pos:self.center_x-10, self.center_y-10
size:[20,20]
on_active: root.custom_click(self, self.active)
size_hint_x: .20
group: "keywords"
这里是 .py 文件的相关部分
class PhysicsModeling(Screen):
kwargPopup = ObjectProperty(None)
physicsModel = ObjectProperty(None)
initialConditions = ObjectProperty(None)
default = ObjectProperty(True)
defaultExpKwargs = 'G0' : 1., 'G1' : .1, 'G2': 1., 'MU':1., 'scaling' : 25, "solution1" : 1,"solution2" : 3, "orientation2" : "x", "y_shift2" : 1./4.
customExpKwargs = ObjectProperty(None)
defaultCheckbox = BooleanProperty(True)
def physicsmodel_spinner_clicked(self, value):
print("Physics Model selected is " + value)
def initialconditions_spinner_clicked(self, value):
print("Intial Conditions " + value + " Selected")
def default_click(self, instance, value):
if value is True:
PhysicsModeling.default = True
print("Checkbox Checked")
else:
PhysicsModeling.default = False
print("Checkbox Unchecked")
def custom_click(self, instance, value):
#todo add popup window for custom kwargs
if value is True:
PhysicsModeling.default = False
print("Checkbox Checked")
PhysicsModeling.default = True
popup = Popup(title='Define Keyword Arguments', content=Label(text='Test Popup'),
auto_dismiss=True, size_hint=(0.5,0.5), pos_hint='x': 0.25,
'y':0.25)
popup.open()
else:
print("Checkbox Unchecked")
def next_button(self):
outputArray[11] = self.physicsModel.text
outputArray[10] = self.initialConditions.text
print("NEXT button pressed!!")
print("Batches Selected ", self.physicsModel.text)
print("Run Solutions ", self.initialConditions.text)
if self.default == True:
outputArray[12] = self.defaultExpKwargs
print("Default KWargs Selected")
else:
print("Custom KWargs are DEFINED")
outputArray[12] = self.customExpKwargs
【问题讨论】:
【参考方案1】:尝试将 CheckBox 活动状态设置为 True 这对我有用 就这样
CheckBox:
id: defaultCheckbox
active: True
【讨论】:
正如我在上面的帖子中所说的I have tried both methods I've seen on other related posts
defaultCheckbox = BooleanProperty(True) #in .py file
and I've tried adding
active: True # in the .kv file
您能否显示一个可以描述您的问题的最少代码,以便我对其进行测试以上是关于如何在 Kivy 中默认选中复选框?的主要内容,如果未能解决你的问题,请参考以下文章