Enable or Disable TexInput based on checkbox click

149 views
Skip to first unread message

rajarame...@gmail.com

unread,
Mar 26, 2021, 10:51:57 AM3/26/21
to Kivy users support
Hi,

I have one TexInput and three checkbox. I am able to enable the TexInput with attached code when i click the checkbox. But the TextInput disabled when i uncheck one of the checkbox(from three checkbox), i need the TextInput to be visible/enable, until i uncheck all checkbox.

Thanks,
Raja 
disable.PNG
enable.PNG
check_box_ex1 - Copy.zip

Elliot Garbus

unread,
Mar 26, 2021, 11:56:18 AM3/26/21
to kivy-...@googlegroups.com

Code attached.

 

Here is the key changes:

 

TextInput:
    id: ti
    hint_text: 'Enter Text'
    size_hint: .2,.08
    pos_hint: {'x': .4, 'y':.3}
    opacity: 0    # In Visible
    disabled: not all([chk_mamidi.active, chk_ramesh.active, chk_raja.active])
    on_disabled: self.opacity = 0 if self.disabled else 1

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/ec9085c5-b07e-4fab-a5d7-4f8fddc0d298n%40googlegroups.com.

 

check_box_ex1 - Copy.zip

Raja Ramesh

unread,
Mar 26, 2021, 12:54:04 PM3/26/21
to kivy-...@googlegroups.com
Hi Elliot,

how to make TextInput visible or invisible for individual checkbox?

Thanks,
Raja

Raja Ramesh

unread,
Mar 26, 2021, 1:09:31 PM3/26/21
to kivy-...@googlegroups.com
Also the TextInput should be visible if two checkbox's are checked and one is unchecked. 

Elliot Garbus

unread,
Mar 26, 2021, 1:47:32 PM3/26/21
to kivy-...@googlegroups.com

Be very specific.  What do you want to achieve?

My interpretation:

If any 2 checkboxes are checked, and one is unchecked, the textbox is visible.

You would need to list out the cases.

 

all([not chk_1.active, chk_2.active, chk_3.active])  or all([chk_1.active, not chk_2.active, chk_3.active]) or all([chk_1.active, chk_2.active, not chk_3.active])

Raja Ramesh

unread,
Mar 29, 2021, 3:01:48 AM3/29/21
to kivy-...@googlegroups.com
Hi Elliot,

whenever user click on raja or ramesh or both (raja, ramesh) checkbox ,Textinput should appear in read mode with current date. which i am trying currently.

I modified my code with the details shared by you as below.  But the TextInput is not in disabled mode (means, it should be in read only  mode. and user should not edit. )  Also this TextInput should contain current date in read mode.
disabled:  all([not chk_ramesh.active, not chk_raja.active])

Thanks,
Raja

editable.PNG
check_box_ex1 - Copy.zip

Elliot Garbus

unread,
Mar 29, 2021, 10:01:28 AM3/29/21
to kivy-...@googlegroups.com

Key changes:

disabled:  not any([chk_ramesh.active, chk_raja.active])

 

Added the date.

class CheckTest(BoxLayout):
   
def on_kv_post(self, base_widget):
       
self.ids.ti.text = date.today().strftime('%x')

 

I also changed the name checkText to CheckTest to conform to Python conventions for class names.

check_box_ex1 - Copy.zip

Raja Ramesh

unread,
Mar 30, 2021, 5:59:25 AM3/30/21
to kivy-...@googlegroups.com
Hi Elliot,

I have updated checkbox and TexInput details in attached files. Is it possible to read checkbox and TextInput details in "InputsBoxLayout" class, whenever checkbox is active / clicked?. so, that it will be easy for me to update the details in an excel file    

Thanks,
Raja

checkbox_ex.zip

Elliot Garbus

unread,
Mar 30, 2021, 9:54:07 AM3/30/21
to kivy-...@googlegroups.com

Here is how you access the desired attributes:

 

def new_user_setup_continue(self, dt):
   
# pass
    # using list
   
app = App.get_running_app()
   
print(app.root.ids.sm.get_screen('First').ids)
    p = app.root.ids.sm.get_screen(
'First').ids
   
print(p.sv_box.children)
   
for c in p.sv_box.children[::-1]:
       
print(f'NamedInput: {c.ids.ti.text}')
   
print(f'{p.chk_rda.active=}')

 

code attached.

checkbox_ex.zip

Raja Ramesh

unread,
Mar 31, 2021, 5:34:08 AM3/31/21
to kivy-...@googlegroups.com
HI Elliot,

how to change color for label text?

Thanks,
Raja

checkbox_ex.zip
change_color.PNG

Elliot Garbus

unread,
Mar 31, 2021, 9:50:22 AM3/31/21
to kivy-...@googlegroups.com

Create a color property in NamedInput, and use it to set the color of the text.  In the code below I have set them all to the same color.  Changes highlighted.

 

from datetime import date

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty, ColorProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen, ScreenManager

kv =
'''
<Mainscreen>:
    name: 'main'
    Label:
        text: 'Select Task '
        font_size: 30
        size_hint: .3,.2
        pos_hint: {'center_x':.5, 'center_y':.9}
    FloatLayout:
        Button:
            text: 'User_Setup'
            size_hint: .2,.1
            pos_hint: {'center_x':.4, 'center_y':.5}
            on_release:
                root.manager.transition.direction= 'left'
                root.manager.current = "First"
        Button:
            text: 'Temp_Setup'
            size_hint: .2,.1
            pos_hint: {'center_x':.6, 'center_y':.5}
            on_release:
                root.manager.transition.direction= 'left'
                root.manager.current = "Second"
<NamedInput>:
    size_hint_y: None
    height: 30
    Label:
        id: label
        text: root.name
        color: root.color

    TextInput:
        id: ti
        hint_text: 'Enter Text'
        write_tab: False
       
<First>:
    name:"First"
    BoxLayout:
        padding: 10
        BoxLayout:  # The left side
        BoxLayout:  # The Center
            orientation: 'vertical'
            spacing: 40
            size_hint_x: None
            width: 300
            Label: # Space at the top
                text: 'Scroll View Test'
                size_hint_y: .2
            ScrollView:
                do_scroll_x: False
                do_scroll_y: True
                scroll_type:['bars', 'content']
                bar_inactive_color: 0,0,0,0.6
                bar_width: 12
                InputsBoxLayout:
                    id: sv_box
                    orientation: 'vertical'
                    # must set the size of the item is the scroll view - or it will not scroll
                    size_hint_y: None            
                    height: self.minimum_height
                                          
        #-----------------------------------------------------------------------
            # Adding below code to test checkbox
            #Label:
            #    text: 'Include:'
            #    size_hint: .1,.2
            #    pos_hint:{'x':.1, 'y':.01}
            FloatLayout:
                #padding: 40
                orientaion:'vertical'
                size_hint_y: None
                #height: 48

                TextInput:
                    id: ti
                    hint_text: 'Enter Text'
                    size_hint: .4,.3
                    pos_hint: {'x': .6, 'y':.1}

                    opacity: 0    # In Visible          
                    disabled:  not any([chk_fu.active, chk_fe.active])

                    on_disabled: self.opacity = 0 if self.disabled else 1
                    readonly: True
                Label:
                    text: 'Include:'
                    size_hint: .1,.2
                    pos_hint:{'x':.06, 'y':.5}
                Label:
                    text: 'RDA'
                    size_hint: .1,.2
                    pos_hint: {'x': .3, 'y':.5}
                    #text_size: self.size
                   #valign: 'middle'
                CheckBox:
                    #group: 'check'
                    id : chk_rda
                    text: "rda"
                    size_hint: .1,.2
                    pos_hint: {'x': .2, 'y':.5}             
                    canvas.before:
                        # applying color for Outer rectangle
                        Color:
                            rgb: 1,0,1
                        Rectangle:
                            pos:self.center_x-8, self.center_y-8
                            size:[15,16]
                        # applying color for Inner rectangle
                        Color:
                            rgb: 0,0,0
                        Rectangle:
                            pos:self.center_x-7, self.center_y-7
                            size:[13,13]
                Label:
                    text: 'File Express'
                    size_hint: .1,.2
                    pos_hint: {'x': .6, 'y':.5}
                    #text_size: self.size
                    #valign: 'middle'
                CheckBox:
                    #group: 'check'
                    id : chk_fe
                    text: "fe"
                    size_hint: .1,.2
                    pos_hint: {'x': .42, 'y':.5}            
                    #on_active: ti
                    #active: False 
                    #on_active: ti.opacity=1
                    #on_active:
                    #    root.on_checkbox_active(*args)   
                    canvas.before:
                        # applying color for Outer rectangle
                        Color:
                            rgb: 1,0,1
                        Rectangle:
                            pos:self.center_x-8, self.center_y-8
                            size:[15,16]
                        # applying color for Inner rectangle
                        Color:
                            rgb: 0,0,0
                        Rectangle:
                            pos:self.center_x-7, self.center_y-7
                            size:[13,13]
                Label:
                    text: 'File Upload'
                    size_hint: .1,.2
                    pos_hint: {'x': .97, 'y':.5}
                    #text_size: self.size
                    #valign: 'middle'
                CheckBox:
                    #group: 'check'
                    id : chk_fu
                    text: "fu"
                    size_hint: .1,.2
                    pos_hint: {'x': .8, 'y':.5}            
                    #on_active: ti
                    #active: False 
                    #on_active: ti.opacity=1
                    #on_active:
                    #    root.on_checkbox_active(*args)   
                    canvas.before:
                        # applying color for Outer rectangle
                        Color:
                            rgb: 1,0,1
                        Rectangle:
                            pos:self.center_x-8, self.center_y-8
                            size:[15,16]
                        # applying color for Inner rectangle
                        Color:
                            rgb: 0,0,0
                        Rectangle:
                            pos:self.center_x-7, self.center_y-7
                            size:[13,13]            
            #-----------------------------------------------------------------------       
            BoxLayout:
                size_hint_y: None
                height: 48
                Button:
                    text:'Back'
                    on_press:
                        root.manager.transition.direction = 'right'
                        root.manager.current = 'main'
                Button:
                    text: 'Next'
                    on_release:
                        sv_box.newUserSetup()        
        BoxLayout: # space at the bottom
    BoxLayout: # The right side           

<Second@Screen>:    # added @Screeen to get code to build
    name:"Second"       
    FloatLayout:
        Label:
            text: 'Client Name:-'
            size_hint: .2, .1
            pos_hint:{'x': .3, 'y': .5}
        TextInput:
            id: client_name
            size_hint: .2, .055
            pos_hint:{'x': .47, 'y': .52}
            hint_text:'Enter Text'
        Label:
            text: 'Email:-'
            size_hint: .2, .1
            pos_hint:{'x': .3, 'y': .4}
        TextInput:
            id: email
            size_hint: .2, .055
            pos_hint:{'x': .47, 'y': .42}
            hint_text:'Enter Text'
        Button:
            text: 'Back'
            size_hint: .18, .08
            pos_hint:{'x': .36, 'y': .30}
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'main'
        Button:
            text: 'Next'
            size_hint: .18, .08
            pos_hint:{'x': .54, 'y': .30}
            on_press:
                root.openChrome()

<mailWaitPopup>:
    #title: 'Please Wait'
    title: ''
    separator_height: 0
    size_hint:(None, None)
    size:(300, 100)
    #on_open: app.sv_box.newUserSetup()
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Please wait...we are building Mail setup!'
           
<webWaitPopup>:   
    title: ''
    separator_height: 0
    size_hint:(None, None)
    size:(300, 100)
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Please wait...IE browser setting-up!'

<closePopup>:
    title: 'Task Completed...!'
    size_hint:(None, None)
    size:(300, 150)
    #on_dismiss: app.sv_box.newUserSetup()
    BoxLayout:
        orientation: 'vertical'       
        Label:
            text: 'Do you want to Repeat the Task?'
        BoxLayout:
            size_hint_y: None
            height: 38  
            Button:
                text: 'Yes'
                #size_hint: .8,.4
                on_press:
                    app.root.ids.sm.get_screen('First')
                    root.dismiss()
            Button:
                text: 'No'
                #size_hint: .8,.4
                on_press: app.stop()

<ErrorPopup>:
    title: ''
    separator_height: 0
    size_hint:(None, None)
    size:(300, 100)
    auto_dismiss: False
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'we got error...!'


BoxLayout:
    orientation: 'vertical'
    ScreenManagment:
        id: sm       
        Mainscreen:
        First:
        Second:
'''


class mailWaitPopup(Popup):
   
pass


class
webWaitPopup(Popup):
   
pass


class
closePopup(Popup):
   
pass


class
ErrorPopup(Popup):
   
pass


class
Mainscreen(Screen):
   
pass


class
ScreenManagment(ScreenManager):
   
pass


class
NamedInput(BoxLayout):
    name = StringProperty()
    color = ColorProperty(
'yellow')


class InputsBoxLayout(BoxLayout):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.long_wait_pop = None
       
self.close_pop = None

    def
on_kv_post(self, base_widget):

       
# self.ids.ti.text = date.today().strftime('%x')
        # self.pop = Popp()
       
self.mail_wait_pop = mailWaitPopup()
       
self.close_pop = closePopup()
       
# -------------------------------------------------------

        # using column names
       
a = ['* a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
       
for i in a:
            w = NamedInput(
name=f'{i}')
           
self.add_widget(w)

   
def print_text(self):
       
for named_input in self.children:
           
print(f'{named_input.ids.label} - {named_input.ids.ti.text}')

   
def print_select_text(self):
       
# using list
       
inputs_list = [ni.ids.ti.text for ni in self.children]
       
print(f'list in reverse order {inputs_list}')
        inputs_list.reverse()
       
print(f'list in correct order {inputs_list}')
       
# to display 2 label value
       
print(f'input value for label 2 is:- {inputs_list[1]}')

       
# using dictionary
       
inputs_dict = {ni.ids.label.text: ni.ids.ti.text for ni in self.children}
       
# print(inputs_dict['Input Name 2'])
        # print(f'Input Name 2: {inputs_dict["Input Name 2"]}')
        # print(f'Input Name 2: {inputs_dict["2"]}')
       
print(f'Input Name 2: {inputs_dict["Company Name"]}')

   
def newUserSetup(self):
       
self.mail_wait_pop.open()
        Clock.schedule_once(
self.new_user_setup_continue)

   
def new_user_setup_continue(self, dt):
       
# pass
        # using list
       
app = App.get_running_app()
       
print(app.root.ids.sm.get_screen('First').ids)
        p = app.root.ids.sm.get_screen(
'First').ids
        
print(p.sv_box.children)
       
for c in p.sv_box.children[::-1]:
           
print(f'NamedInput: {c.ids.ti.text}')
       
print(f'{p.chk_rda.active=}')



       
# inputs_list = [ni.ids.ti.text for ni in self.children]
        # inputs_list.reverse()
        # b = inputs_list[0]
        # print(f'value of b is:- {b}')
        # using dictionary
        # inputs_dict = {ni.ids.label.text: ni.ids.ti.text for ni in self.children}
        # a = {inputs_dict["a"]}
        # print(f'value of a is:- {a}')
        # ------------------------------------------------
        # for names in inputs_dict:
        #    print(names)
        # ------------------------------------------------
        # import openpyxl as xl
        # from openpyxl.styles import Font
        # wb = xl.Workbook()
        # ws = wb.active
        # ws['A1'] = 'First Name'
        # ws['B1'] = 'Last Name'
        # for row in ws.iter_rows(min_col=1, min_row=1, max_col=ws.max_column, max_row=ws.max_row):
        #     print(row)
        #     for val in row:
        #         val.font = Font(bold=True, name='Tahoma', size=10)
        #         ws['A2'] = inputs_list[0]  # using list
        #         ws['B2'] = inputs_list[1]
        #         wb.save('excel_details.xlsx')
        # # Eraising TextInputs fields
        # for ni in self.children:
        #     ni.ids.ti.text = ''
        #
        # # calling popup.dismiss
       
self.mail_wait_pop.dismiss()
       
self.close_pop.open()


class First(Screen):
   
# def on_checkbox_active(self, checkbox, value):
    #    if value:
    #        # self.ids.ti.enabled = False  # to enable the textinput
    #        self.ids.ti.disabled = True # to disable the textinput
    #        self.ids.ti.opacity = 1
    #    else:
    #        #self.ids.ti.enabled = False
    #        self.ids.ti.opacity = 0

   
def on_kv_post(self, base_widget):
       
self.ids.ti.text = date.today().strftime('%x')

   
# pass


class MainApp(App):
   
def build(self):
       
return Builder.load_string(kv)


MainApp().run()

Raja Ramesh

unread,
Apr 28, 2021, 5:54:50 AM4/28/21
to kivy-...@googlegroups.com
Hi Elliot,

Is it possible to have a margin / border for layouts( BoxLayout / FloatLayout)? if yes, could you please share an example?

Thanks,
Raja

for_margin.PNG

Elliot Garbus

unread,
Apr 28, 2021, 8:35:38 AM4/28/21
to kivy-...@googlegroups.com

Use the canvas to draw a line around the layout.

 

from kivy.app import App
from kivy.lang import Builder

kv =
"""
<TwoButtonsBoxed@BoxLayout>:
    padding: 15
    canvas:
        Color:
            rgba: 1, .2, .3, 1
        Line:
            width: 2
            rectangle: (*self.pos, *self.size)
    Button:
        text: 'Left'
    Button:
        text: 'Right'

BoxLayout:
    orientation: 'vertical'
    padding: 20
    spacing: 30
    TwoButtonsBoxed:
    TwoButtonsBoxed:
"""


class DrawBoxApp(App):
   
def build(self):
       
return Builder.load_string(kv)

DrawBoxApp().run()

Raja Ramesh

unread,
Jun 4, 2021, 7:04:25 AM6/4/21
to kivy-...@googlegroups.com
Hi Elliot,

I have a check box which is active by default and I used 'disable: True' not to edit / uncheck the checkbox by user. But this is not working. so, how to make the checkbox not to edit / uncheck by the user when it is active by default.
CheckBox:
    id : chk_fe
text: "fe"
size_hint: .1,.2
    pos_hint: {'x': .2, 'y':.5}             
active: True # to make the checkbox checked by default
#disable: True

Thanks,
Raja

You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/4Qp2vjI4IsE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/60895694.1c69fb81.1c4df.af25SMTPIN_ADDED_MISSING%40gmr-mx.google.com.

Elliot Garbus

unread,
Jun 4, 2021, 9:04:23 AM6/4/21
to kivy-...@googlegroups.com

Raja Ramesh

unread,
Jun 4, 2021, 11:36:09 AM6/4/21
to kivy-...@googlegroups.com
Hi Elliot,

Is it possible to change the color of the check mark using Color property in the checkbox? 

Thanks,
Raja 

Elliot Garbus

unread,
Jun 4, 2021, 11:49:01 AM6/4/21
to kivy-...@googlegroups.com
As I recall the color will get applied to both the outline and the check. It is easy enough to try.  I know on some projects I have replaced the 2 images. 

Sent from my iPhone

Raja Ramesh

unread,
Jun 4, 2021, 12:36:46 PM6/4/21
to kivy-...@googlegroups.com
Hi Elliot,

I am able to change the color of the checkbox but not able to change the color of outline and the check. Any example would be helpful.

Thanks,
Raja

Elliot Garbus

unread,
Jun 4, 2021, 1:05:07 PM6/4/21
to kivy-...@googlegroups.com
As I recall there are 2 images. The empty box, and the box with a check Mark in it. You will need to create new images to use. 


Sent from my iPhone

ElliotG

unread,
Jun 4, 2021, 2:39:49 PM6/4/21
to Kivy users support
Here is an example, I have included 2 images I created.  In my app I wanted the border of the check box to be white, and bolder than the default.
You can take these into an image editor can change the colors. 

The color attribute does change the color of both the box and the check.  The effect depends on the color. 
cbox.zip

ElliotG

unread,
Jun 4, 2021, 2:40:48 PM6/4/21
to Kivy users support
If you change the color of the images I sent to white, you can then use color to set the color of the box and check.
Reply all
Reply to author
Forward
0 new messages