• <legend id='5kgqh'><style id='5kgqh'><dir id='5kgqh'><q id='5kgqh'></q></dir></style></legend>

      <small id='5kgqh'></small><noframes id='5kgqh'>

    1. <tfoot id='5kgqh'></tfoot>
        <i id='5kgqh'><tr id='5kgqh'><dt id='5kgqh'><q id='5kgqh'><span id='5kgqh'><b id='5kgqh'><form id='5kgqh'><ins id='5kgqh'></ins><ul id='5kgqh'></ul><sub id='5kgqh'></sub></form><legend id='5kgqh'></legend><bdo id='5kgqh'><pre id='5kgqh'><center id='5kgqh'></center></pre></bdo></b><th id='5kgqh'></th></span></q></dt></tr></i><div id='5kgqh'><tfoot id='5kgqh'></tfoot><dl id='5kgqh'><fieldset id='5kgqh'></fieldset></dl></div>
          <bdo id='5kgqh'></bdo><ul id='5kgqh'></ul>

        获取kivy中选中复选框的值

        get value of selected checkbox in kivy(获取kivy中选中复选框的值)

          <small id='DcOCs'></small><noframes id='DcOCs'>

          • <bdo id='DcOCs'></bdo><ul id='DcOCs'></ul>

              • <legend id='DcOCs'><style id='DcOCs'><dir id='DcOCs'><q id='DcOCs'></q></dir></style></legend>
                  <tbody id='DcOCs'></tbody>

                <i id='DcOCs'><tr id='DcOCs'><dt id='DcOCs'><q id='DcOCs'><span id='DcOCs'><b id='DcOCs'><form id='DcOCs'><ins id='DcOCs'></ins><ul id='DcOCs'></ul><sub id='DcOCs'></sub></form><legend id='DcOCs'></legend><bdo id='DcOCs'><pre id='DcOCs'><center id='DcOCs'></center></pre></bdo></b><th id='DcOCs'></th></span></q></dt></tr></i><div id='DcOCs'><tfoot id='DcOCs'></tfoot><dl id='DcOCs'><fieldset id='DcOCs'></fieldset></dl></div>
              • <tfoot id='DcOCs'></tfoot>
                  本文介绍了获取kivy中选中复选框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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()
                  

                  test.kv

                  <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()
                  

                  如何获取复选框的值?我正在使用 age.text 获取年龄文本框的值,但我不知道复选框的值?
                  单击确定"时,如何获取选中的复选框值并传入 root.insert_data.

                  How to get value of checkbox?I am getting value of age textbox using age.text but checkbox value i don't know?
                  When click on 'Ok' then How to get selected checkbox value and pass in root.insert_data.

                  推荐答案

                  您可以通过其 active 属性来获取复选框的选中状态,因此请尝试更改:

                  You can get the checked state of a checkbox with its active property, so try change:

                  GreenButton:
                      text: 'Ok'
                      on_press: root.insert_data(chk.active ,age.text)
                  

                  在此代码段中,chk.text 已更改为 chk.active,这对我很有效.

                  In this snippet chk.text was changed to chk.active which works for me properly.

                  在 https://kivy.org/docs 查看更多关于 kivy 复选框的参考资料/api-kivy.uix.checkbox.html

                  希望对您有所帮助.试试看吧.

                  Hope it helps. Give it a try.

                  因此,为了能够获取每个复选框的属性和文本输入,您可以将 ObjectProperties 分配给小部件,并且可以将它们链接到您的 test.py文件.

                  So in order to be able to get the properties of each checkbox and the text input you can assign ObjectProperties to the widgets, and you can link them to your test.py file.

                  修改后的来源:

                  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()
                  

                  .py 文件中,您可以找到 ObjectProperty 的新导入.您还可以看到,在 UserGroup 中定义了三个新属性来与视图交互,并且在 UserGroup.insert_data 中的修改非常简单.

                  In the .py file you can find a new import of ObjectProperty. Also you can see that three new properties were defined in UserGroup to interact with the view, and the modifications in UserGroup.insert_data are straightforward.

                  <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()
                  

                  .kv 文件中,两个复选框的 id 和文本输入被重命名为 chk_malechk_femaletxt_age 分别.

                  In the .kv file the ids of the two checkboxes and the text input are renamed to chk_male, chk_female and txt_age respectively.

                  您还可以看到,对象属性链接是在 UserGroup 部分的开头定义的.

                  Also you can see that the object property links are defined at the beginning of the UserGroup section.

                  希望它有意义并符合您的要求.

                  Hope it makes sense and match your requirements.

                  这篇关于获取kivy中选中复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                  <legend id='hEZLO'><style id='hEZLO'><dir id='hEZLO'><q id='hEZLO'></q></dir></style></legend>
                  <i id='hEZLO'><tr id='hEZLO'><dt id='hEZLO'><q id='hEZLO'><span id='hEZLO'><b id='hEZLO'><form id='hEZLO'><ins id='hEZLO'></ins><ul id='hEZLO'></ul><sub id='hEZLO'></sub></form><legend id='hEZLO'></legend><bdo id='hEZLO'><pre id='hEZLO'><center id='hEZLO'></center></pre></bdo></b><th id='hEZLO'></th></span></q></dt></tr></i><div id='hEZLO'><tfoot id='hEZLO'></tfoot><dl id='hEZLO'><fieldset id='hEZLO'></fieldset></dl></div>

                          <small id='hEZLO'></small><noframes id='hEZLO'>

                            <tbody id='hEZLO'></tbody>
                            <bdo id='hEZLO'></bdo><ul id='hEZLO'></ul>
                            <tfoot id='hEZLO'></tfoot>