Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to enable/disable editing in TextInput using kivy in python

I have a piece of code. (1) The TextInput value should be shown , but first it should not be editable, after clicking the corresponding CheckBox, the TextInput will be editable.
(2) Using the iteration, the Label and the TextInput should get the value. The value at Label and TextInput should not be hard coded(although it's there in my code, @FJSevilla helped me for this one).
(3) However, the values of Label and TextInput are stored in a variable in json format. something like this(you can consider like key,value pair in map) [ variable = '{"a" : " Goc" , "b" : "Coc", "c" : "Dow" } '] (you can see diagram for more clearance). I appreciate the help.

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder

Builder.load_string("""

<Test>:
    do_default_tab: False

    TabbedPanelItem:
        text: 'page1'

        BoxLayout:
            padding: 50, 50, 50, 50
            orientation: 'horizontal'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                size_hint_x: 1
                Label:
                    text: 'a'
                Label:
                    text: 'b'
                Label:
                    text: 'c'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                TextInput:
                    text: 'Goc'
                TextInput:
                    text: 'Coc'
                TextInput:
                    text: 'Dow'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                size_hint_x: 0.40
                CheckBox: 
                    text: 'CheckBox'
                CheckBox: 
                    text: 'CheckBox'
                CheckBox: 
                    text: 'CheckBox'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                size_hint_x: 0.60
                Button:
                    text: 'save'
                Button:
                    text: 'save'
                Button:
                    text: 'save'


""")

class Test(TabbedPanel):
    pass

class MyApp(App):

    def build(self):
        test = Test()
        return test


if __name__ == '__main__':
    MyApp().run()

kivy userInterface

like image 871
crazyDelight Avatar asked Jul 21 '17 12:07

crazyDelight


People also ask

How do I add TextInput to KIVY?

You can control which text can be added to the TextInput by overwriting TextInput. insert_text() . Every string that is typed, pasted or inserted by any other means into the TextInput is passed through this function. By overwriting it you can reject or change unwanted characters.

How do I change the size of TextInput in KIVY?

in addition to the answer size_hint_x and size_hint_y must be set to None respectively before the height and width attribute can be used i.e size_hint: (None, None) for less typing. If you want to set the width attribute, size_hint_x is set to None and vice-versa.


Video Answer


1 Answers

First of all, thank you for providing an app which was easy to work with.

I tried to implement what you were looking for except the JSON. I am using a simple list, it should be straightforward to extend my code for a JSON.

Instead of using colums, I am using rows which makes it easier to link together the properties of the label textinput and checkbox.

enter image description here

    from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.lang import Builder

ROWS = ['Goc', 'COC', 'EEE']

Builder.load_string("""

<Test>:
    do_default_tab: False

    TabbedPanelItem:
        text: 'page1'

        Table:
            padding: 50, 50, 50, 50
            orientation: 'vertical'

<Row>:
    spacing: 50
    #orientation: 'vertical'
    size_hint_x: 1
    txt: txtinpt.text
    Label:
        text: root.txt
    TextInput:
        id: txtinpt
        text: root.txt
        disabled: not CheckBox.active
    CheckBox:
        id:CheckBox 
        text: 'CheckBox'
        active: False
    Button:
        text: 'save'

""")
class Table(BoxLayout):
    def __init__(self, **kwargs):
        super(Table, self).__init__(**kwargs)
        for row in ROWS:
            self.add_widget(Row(row))



class Row(BoxLayout):
    txt = StringProperty()
    def __init__(self, row, **kwargs):
        super(Row, self).__init__(**kwargs)
        self.txt = row



class Test(TabbedPanel):
    pass

class MyApp(App):

    def build(self):
        test = Test()
        return test


if __name__ == '__main__':
    MyApp().run()
like image 101
PalimPalim Avatar answered Sep 16 '22 14:09

PalimPalim