Python 获取kivy中选中复选框的值 test.py 试验电压(千伏) : 文本大小:self.size valign:“中间” 填充x:5 : 多行:False : 背景颜色:1,1,1,1 尺寸提示:无 高度:self.parent.height*0.120 用户组 网格布局: 科尔斯:2 填充:30,30 间距:20,20 行\默认\高度:“30dp” 标签: 文字:“男性” 文本大小:self.size valign:“中间” 复选框: 组:'检查' id:chk 标签: 文字:“女性” 文本大小:self.size valign:“中间” 复选框: 组:'检查' 自定义标签: 文字:“年龄” 文本大小:self.size valign:“中间” SingleLineTextInput: id:年龄 绿色按钮: 文字:“Ok” 按:根。插入数据(chk.text,age.text) 绿色按钮: 文本:“取消” 按:app.stop()
如何获取复选框的值?我正在使用age.text获取年龄文本框的值,但我不知道复选框的值Python 获取kivy中选中复选框的值 test.py 试验电压(千伏) : 文本大小:self.size valign:“中间” 填充x:5 : 多行:False : 背景颜色:1,1,1,1 尺寸提示:无 高度:self.parent.height*0.120 用户组 网格布局: 科尔斯:2 填充:30,30 间距:20,20 行\默认\高度:“30dp” 标签: 文字:“男性” 文本大小:self.size valign:“中间” 复选框: 组:'检查' id:chk 标签: 文字:“女性” 文本大小:self.size valign:“中间” 复选框: 组:'检查' 自定义标签: 文字:“年龄” 文本大小:self.size valign:“中间” SingleLineTextInput: id:年龄 绿色按钮: 文字:“Ok” 按:根。插入数据(chk.text,age.text) 绿色按钮: 文本:“取消” 按:app.stop(),python,python-2.7,kivy,kivy-language,Python,Python 2.7,Kivy,Kivy Language,如何获取复选框的值?我正在使用age.text获取年龄文本框的值,但我不知道复选框的值 单击“确定”后,如何获取选中的复选框值并传入根。插入数据。您可以通过复选框的活动属性获取复选框的选中状态,因此请尝试更改: GreenButton: text: 'Ok' on_press: root.insert_data(chk.active ,age.text) 在这段代码中,chk.text被更改为chk.active,这对我来说很合适 有关kivy复选框的更多参考信息,请访问 希望
单击“确定”后,如何获取选中的复选框值并传入根。插入数据。
您可以通过复选框的
活动属性获取复选框的选中状态,因此请尝试更改:
GreenButton:
text: 'Ok'
on_press: root.insert_data(chk.active ,age.text)
在这段代码中,chk.text
被更改为chk.active
,这对我来说很合适
有关kivy复选框的更多参考信息,请访问
希望能有帮助。试试看
更新:
因此,为了能够获得每个复选框的属性和文本输入,您可以将ObjectProperties
分配给小部件,并将它们链接到test.py
文件
修改后的资料来源:
test.py
在.py
文件中,您可以找到新导入的ObjectProperty
。
您还可以看到,在UserGroup
中定义了三个与视图交互的新属性,UserGroup.insert_data
中的修改非常简单
试验电压(千伏)
:
文本大小:self.size
valign:“中间”
填充x:5
:
多行:False
:
背景颜色:1,1,1,1
尺寸提示:无
高度:self.parent.height*0.120
用户组
男:chk_男
女:chk_女
年龄:txt_年龄
网格布局:
科尔斯:2
填充:30,30
间距:20,20
行\默认\高度:“30dp”
标签:
文字:“男性”
文本大小:self.size
valign:“中间”
复选框:
组:'检查'
身份证号码:chk_男
标签:
文字:“女性”
文本大小:self.size
valign:“中间”
复选框:
组:'检查'
身份证号码:chk_女
自定义标签:
文字:“年龄”
文本大小:self.size
valign:“中间”
SingleLineTextInput:
id:txt_年龄
绿色按钮:
文字:“Ok”
按:root.insert_data()
绿色按钮:
文本:“取消”
按:app.stop()
在.kv
文件中,两个复选框的ID和文本输入分别重命名为chk_-male
、chk_-male
和txt_-age
您还可以看到,对象属性链接定义在UserGroup
部分的开头
希望它有意义并符合您的要求。解决方案是使用StringProperty,将文本(男性、女性)添加到复选框中,并在活动中捕获性别
test.py
试验电压(千伏)
例子
test.py
试验电压(千伏)
#:kivy 1.10.0
:
文本大小:self.size
valign:“中间”
填充x:5
:
多行:False
:
背景颜色:1,1,1,1
尺寸提示:无
高度:self.parent.height*0.120
用户组
网格布局:
科尔斯:2
填充:30,30
间距:20,20
行\默认\高度:“30dp”
标签:
文字:“男性”
文本大小:self.size
valign:“中间”
复选框:
组:'检查'
id:chk
正文:“男性”
激活时:
root.gender=self.text
标签:
文字:“女性”
文本大小:self.size
valign:“中间”
复选框:
组:'检查'
文本:“女性”
激活时:
root.gender=self.text
自定义标签:
文字:“年龄”
文本大小:self.size
valign:“中间”
SingleLineTextInput:
id:年龄
绿色按钮:
文字:“Ok”
按:root.insert\u数据(age.text)
绿色按钮:
文本:“取消”
按:app.stop()
输出
您是否尝试过chk.active
?请参阅作为参考。您能再帮我一个忙吗?它的打印值为true false,但我需要男性值和女性值。您将复选框分组,以便一次只能激活一个复选框chk
是男性复选框的id
,因此当您获得True
时,男性被选中,当您获得False
时,女性被选中。如果我没有选择任何人,那么也将获得False值。@gunjantalwar是的,我将更新我的答案,为您介绍一种不同且更稳健的方法。
<CustomLabel@Label>:
text_size: self.size
valign: "middle"
padding_x: 5
<SingleLineTextInput@TextInput>:
multiline: False
<GreenButton@Button>:
background_color: 1, 1, 1, 1
size_hint_y: None
height: self.parent.height * 0.120
UserGroup
GridLayout:
cols: 2
padding : 30,30
spacing: 20, 20
row_default_height: '30dp'
Label:
text: 'Male'
text_size: self.size
valign: 'middle'
CheckBox:
group: 'check'
id : chk
Label:
text: 'Female'
text_size: self.size
valign: 'middle'
CheckBox:
group: 'check'
CustomLabel:
text: 'age'
text_size: self.size
valign: 'middle'
SingleLineTextInput:
id: age
GreenButton:
text: 'Ok'
on_press: root.insert_data(chk.text,age.text)
GreenButton:
text: 'Cancel'
on_press: app.stop()
GreenButton:
text: 'Ok'
on_press: root.insert_data(chk.active ,age.text)
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import ObjectProperty
Window.size = (600, 325)
class UserGroup(Screen):
male = ObjectProperty(None)
female = ObjectProperty(None)
age = ObjectProperty(None)
def insert_data(self):
if self.male.active:
print('Male')
elif self.female.active:
print('Female')
else:
print('No gender selected')
print(self.age.text)
class FactUserGroup(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
FactUserGroup().run()
<CustomLabel@Label>:
text_size: self.size
valign: "middle"
padding_x: 5
<SingleLineTextInput@TextInput>:
multiline: False
<GreenButton@Button>:
background_color: 1, 1, 1, 1
size_hint_y: None
height: self.parent.height * 0.120
UserGroup
male: chk_male
female: chk_female
age: txt_age
GridLayout:
cols: 2
padding : 30,30
spacing: 20, 20
row_default_height: '30dp'
Label:
text: 'Male'
text_size: self.size
valign: 'middle'
CheckBox:
group: 'check'
id : chk_male
Label:
text: 'Female'
text_size: self.size
valign: 'middle'
CheckBox:
group: 'check'
id: chk_female
CustomLabel:
text: 'age'
text_size: self.size
valign: 'middle'
SingleLineTextInput:
id: txt_age
GreenButton:
text: 'Ok'
on_press: root.insert_data()
GreenButton:
text: 'Cancel'
on_press: app.stop()
from kivy.properties import StringProperty
...
def insert_data(self, age):
print("Gender={}".format(self.gender))
print("Age={}".format(age))
CheckBox:
group: 'check'
id : chk
text: "Male"
on_active:
root.gender = self.text
...
CheckBox:
group: 'check'
text: "Female"
on_active:
root.gender = self.text
...
GreenButton:
text: 'Ok'
on_press: root.insert_data(age.text)
import sqlite3 as lite
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import StringProperty
Window.size = (600, 325)
class UserGroup(Screen):
gender = StringProperty("")
def insert_data(self, age):
print("Gender={}".format(self.gender))
print("Age={}".format(age))
class FactUserGroup(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
FactUserGroup().run()
#:kivy 1.10.0
<CustomLabel@Label>:
text_size: self.size
valign: "middle"
padding_x: 5
<SingleLineTextInput@TextInput>:
multiline: False
<GreenButton@Button>:
background_color: 1, 1, 1, 1
size_hint_y: None
height: self.parent.height * 0.120
UserGroup
GridLayout:
cols: 2
padding : 30,30
spacing: 20, 20
row_default_height: '30dp'
Label:
text: 'Male'
text_size: self.size
valign: 'middle'
CheckBox:
group: 'check'
id : chk
text: "Male"
on_active:
root.gender = self.text
Label:
text: 'Female'
text_size: self.size
valign: 'middle'
CheckBox:
group: 'check'
text: "Female"
on_active:
root.gender = self.text
CustomLabel:
text: 'age'
text_size: self.size
valign: 'middle'
SingleLineTextInput:
id: age
GreenButton:
text: 'Ok'
on_press: root.insert_data(age.text)
GreenButton:
text: 'Cancel'
on_press: app.stop()