2 python python-2.7 kivy kivy-language
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
Window.size = (600, 325)
class UserGroup(Screen):
def insert_data(self, arg1,arg2):
print(arg1)
print(arg2)
class FactUserGroup(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
FactUserGroup().run()
Run Code Online (Sandbox Code Playgroud)
<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()
Run Code Online (Sandbox Code Playgroud)
如何获取复选框的值?我正在使用 age.text 获取年龄文本框的值,但我不知道复选框的值?
当单击“确定”时,如何获取选定的复选框值并传入 root.insert_data。
您可以通过其active属性获取复选框的选中状态,因此请尝试更改:
GreenButton:
text: 'Ok'
on_press: root.insert_data(chk.active ,age.text)
Run Code Online (Sandbox Code Playgroud)
在这个片段中chk.text被更改为chk.active适合我的。
在https://kivy.org/docs/api-kivy.uix.checkbox.html查看更多关于 kivy 复选框的参考
希望能帮助到你。试一试。
因此,为了能够获得每个复选框的属性和文本输入,您可以分配ObjectProperties给小部件,并且可以将它们链接到您的test.py文件。
修改后的来源:
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()
Run Code Online (Sandbox Code Playgroud)
在该.py文件中,您可以找到新导入的ObjectProperty. 您还可以看到在 中定义了三个新属性UserGroup以与视图交互,并且 中的修改UserGroup.insert_data很简单。
<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()
Run Code Online (Sandbox Code Playgroud)
在.kv文件中,两个复选框的 id 和文本输入分别重命名为chk_male,chk_female和txt_age。
您还可以看到对象属性链接是在该UserGroup部分的开头定义的。
希望它有意义并符合您的要求。
解决方案是使用StringProperty,将文本(男、女)添加到 CheckBox,并使用on_active事件来捕获性别。
from kivy.properties import StringProperty
...
def insert_data(self, age):
print("Gender={}".format(self.gender))
print("Age={}".format(age))
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
#: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()
Run Code Online (Sandbox Code Playgroud)