Add TextInput Widget on screen based on button click

537 views
Skip to first unread message

rajarame...@gmail.com

unread,
Feb 19, 2021, 11:21:34 AM2/19/21
to Kivy users support
Hi 

Is it possible to create a TextInput Widget on a screen(highlighted area of the attachment) whenever we click on a button. TextInput label should be button text. Also need to read the text of label and TextInput. I am aware of creating buttons (attached py file)but not aware how to create TextInput by clicking Button.
 

Thanks,
Raja
Capture.PNG
raja_1 - Copy.py

Elliot Garbus

unread,
Feb 19, 2021, 8:02:04 PM2/19/21
to kivy-...@googlegroups.com

Here is an example below.  Notice I simplified your Buttonbox layout to simply use a BoxLayout and let the horizontal box layout position and size the buttons.  I added 2 labels on the left and right side of the buttons as placeholders, to leave the space you had in your design.

 

Pressing button A instances the TextAndLabel class and adds it to the BoxLayout ti_box.  Type in the text box and the text will appear in the label below the textbox.

 

 

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

kv =
'''
<TextAndLabel>:
    orientation: 'vertical'
    padding: 20
    spacing: 10
    TextInput:
        id: ti
    Label
        text: ti.text
        text_size: self.size
        halign: 'left'
        valign: 'top'


<ButtonBox@BoxLayout>:
    size_hint_y: None
    height: 48
    Label: # Place holder
    Button:
        text: 'A'
        on_press: app.add_textbox()
    Button:
        text: 'B'
        on_press:
    Button:
        text: 'C'
        on_press:
    Button:
        text: 'D'
        on_press:
    Button:
        text: 'E'
        on_press:
    Button:
        text: 'F'
        on_press:
    Button:
        text: 'G'
        on_press:
    Button:
        text: 'H'
        on_press:
    Label: # Placeholder

BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        orientation: 'vertical'
        size_hint_y: None
        height: 100
        Label:
            text: 'Click a Button'
            font_size: 20
            size_hint_y: None
            height: 40
        ButtonBox:
    BoxLayout:
        padding: 40
        orientation: 'vertical'
        id: ti_box
   
'''


class TextAndLabel(BoxLayout):
   
pass


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

   
def add_textbox(self):
       
self.root.ids.ti_box.add_widget(TextAndLabel()) 


MyApp().run()

--
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/907b2577-4370-4a17-ab5f-c36942c6a489n%40googlegroups.com.

 

Raja Ramesh

unread,
Feb 20, 2021, 9:16:20 AM2/20/21
to kivy-...@googlegroups.com
Thank you Elliot....i will try and get back to you.

Raja Ramesh

unread,
Feb 21, 2021, 5:35:21 AM2/21/21
to kivy-...@googlegroups.com
Hi Elliot,

how to display button text along with TexInput when we press on a button. For example, if  i press on button 'A'. 'A' should display beside TextInput. 

Thanks,
Raja
Capture.PNG

Mr. Mog

unread,
Feb 21, 2021, 5:52:40 AM2/21/21
to kivy-...@googlegroups.com
You can give a label besides the TextInput text during on_release of the button you want its text displayed:
...
Button:
    text: "A"
    on_release:
        label.text = self.text

LabelBesidesTI:
    id: label
...

Raja Ramesh

unread,
Feb 21, 2021, 10:25:15 AM2/21/21
to kivy-...@googlegroups.com
Hi Mog,

I have updated the code as below with the details shared by you and button text is appearing beside  Buttons[A, B, C..] instead of appearing beside TextInput. Also i have tried as the code attached but it's not working as expected.

<ButtonBox@BoxLayout>:
size_hint_y: None
height: 48
Label: # Place holder
        id: label  
Button:
id: A
text: 'A'
on_press:
app.add_textbox()
label.text = self.text
Button:
text: 'B'
on_press:
app.add_textbox()
label.text = self.text
Button:
text: 'C'
on_press:
app.add_textbox()
label.text = self.text
Thanks,
Raja

raja_1 - Copy.py
wrong_place.PNG

Elliot Garbus

unread,
Feb 21, 2021, 11:37:05 AM2/21/21
to kivy-...@googlegroups.com

The Label needs to be added to the TextAndLabel class.   A StringProperty, text, was added to hold the value.

The method add_text_box(), was changed to have a text value passed in, and sets the text value.

 

You will need to remove widget when pressing buttons,( or create a number of screens and switch between them, or a scrollview…).  Currently pressing multiple buttons causes the widgets to stack.

 

 

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

kv =
'''

<TextAndLabel>:
    orientation: 'vertical'
    padding: 20
    spacing: 10
    BoxLayout:
        Label:
            text: root.text
            size_hint_x: .1
        TextInput:
            id: ti

    Label
        text: ti.text
        text_size: self.size
        halign: 'left'
        valign: 'top'
       
    #Button:
    #    text: 'print'
    #    on_press: root.print_textbox()
<LabelBesidesTI>:
    id:label

       
<ButtonBox@BoxLayout>:
    size_hint_y: None
    height: 48
    Label: # Place holder
    #LabelBesidesTI:

    Button:
        id: A
        text: 'A'
        on_press:
            app.add_textbox(self.text)
    Button:
        text: 'B'
        on_press:
            app.add_textbox(self.text)
    Button:
        text: 'C'
        on_press:
            app.add_textbox(self.text)
    Button:
        text: 'D'
        on_press:
            app.add_textbox(self.text)
    Button:
        text: 'E'
        on_press:
            app.add_textbox(self.text)
    Button:
        text: 'F'
        on_press:
            app.add_textbox(self.text)
    Button:
        text: 'G'
        on_press:
            app.add_textbox(self.text)
    Button:
        text: 'H'
        on_press:
            app.add_textbox(self.text)

    Label: # Placeholder
   

BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        orientation: 'vertical'
        size_hint_y: None
        height: 100
        Label:
            text: 'Click a Button'
            font_size: 20
            size_hint_y: None
            height: 40
        ButtonBox:    
    BoxLayout:
        padding: 40
        orientation: 'vertical'
        id: ti_box
    BoxLayout:
        BoxLayout:
        BoxLayout:
            padding:20
            orientation:'horizontal'
            spacing: 10
            size_hint_y: None
            width: 300
            Button:
                text:'Next'
                on_press: print('Ready to run the process.')
            Button:
                text:'Close'
                on_press: app.stop()
        BoxLayout:
'''


class TextAndLabel(BoxLayout):
    text = StringProperty()
   
# def read(self):
    #    print(self.ids.label.text +' '+self.ids.ti.text)
   
pass


class
LabelBesidesTI(BoxLayout, Label):
   
pass


class
MyApp(App):

   
def build(self):
       
return Builder.load_string(kv)

   
def add_textbox(self, text):
       
self.root.ids.ti_box.add_widget(TextAndLabel(text=text))
       
# self.labels.ids.label.text.add_widget(LabelBesidesTI())

    # def delete_textbox(self):
    #    print(self.ids.ti.text)


MyApp().run()

Raja Ramesh

unread,
Feb 22, 2021, 12:34:04 AM2/22/21
to kivy-...@googlegroups.com
Thank you Elliot....i will try and get back to you.

Raja Ramesh

unread,
Feb 22, 2021, 1:45:31 AM2/22/21
to kivy-...@googlegroups.com
Hi Elliot,

As per my understanding i have added the scrollview under BoxLayout as below and the same attached to the mail . But it's not working...  Also i have added the same scrollview in "TextAndLabel" class and it is not working... 

BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: 100
Label:
text: 'Click a Button'
font_size: 20
size_hint_y: None
height: 40
ButtonBox:
BoxLayout:
padding: 40
orientation: 'vertical'
        ScrollView:
do_scroll: False, True
bar_width: dp(10)
size_hint_y: None
height: 30 * 3
scroll_type: ['bars','content']
BoxLayout:
orientation: 'vertical'
id:ti_box
spacing: dp(2)
size_hint_y: None
height: self.minimum_height
#id: ti_box


On Sun, Feb 21, 2021 at 10:07 PM Elliot Garbus <elli...@cox.net> wrote:
raja_1 - Copy.py

Elliot Garbus

unread,
Feb 22, 2021, 9:40:25 AM2/22/21
to kivy-...@googlegroups.com

I changed the scrollview. In short you needed to size the items going into the ScrollView.

I removed the sizing of the ScrollView itself so it would fill the screen.  I sized the buttons on the bottom of the screen.

I also updated the delete_textbook() method.  I passed in the widget, and deleted that… additional comments in the code. 

Other changes made.

raja_1_2202.zip

Raja Ramesh

unread,
Feb 22, 2021, 10:42:04 AM2/22/21
to kivy-...@googlegroups.com
Thank you very much Elliot... i will try and get back to you.

Raja Ramesh

unread,
Feb 22, 2021, 12:23:34 PM2/22/21
to kivy-...@googlegroups.com
Hi Elliot,

i am trying to call "TextAndLabel" class by clicking " Next" button using ids as below. but i am getting below error  

kv = '''
<TextAndLabel>:
id:tl
size_hint_y: None
height: 48 # Fix the size of the widget going into the ScrollView
# padding: 20
# spacing: 10
Label:
id:lab
        text: root.text
size_hint_x: .1
TextInput:
id: ti
    Button:
text: 'Delete'
size_hint_x: .1
on_press:
#root.read()
root.getAll()
print('going to delete')
app.delete_textbox(root) # send the widget you want deleted
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: 100
Label:
text: 'Click a Button'
font_size: 20
size_hint_y: None
height: 40
ButtonBox:
BoxLayout:
padding: 40
orientation: 'vertical'
ScrollView:
do_scroll: False, True
bar_width: dp(10)
            # size_hint_y: None        # Let this fill the space
# height: 30 * 3
            scroll_type: ['bars','content']
BoxLayout:
orientation: 'vertical'
id:ti_box
spacing: dp(2)
size_hint_y: None
height: self.minimum_height
#id: ti_box
    BoxLayout:
size_hint_y: None
height: 75 # Set the height for the bottom row
        BoxLayout:
BoxLayout:
padding:20
orientation:'horizontal'
spacing: 10
            # size_hint_y: None
# width: 300
            Button:
text:'Next'
on_press:
print('Ready to run the process.')
                    app.ids.tl.getAll()
            Button:
text:'Close'
on_press: app.stop()
BoxLayout:
'''
class TextAndLabel(BoxLayout):
text = StringProperty()
    def read(self):
print(f'Label "{self.ids.lab.text}" has "{self.ids.ti.text}"')
def getAll(self):
inputs_list = [ni.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}')

pass
class MyApp(App):

def build(self):
return Builder.load_string(kv)

def add_textbox(self, text):
self.root.ids.ti_box.add_widget(TextAndLabel(text = text))
#self.labels.ids.label.text.add_widget(LabelBesidesTI())
        #print(self.check.ids.ti.text)

def delete_textbox(self, w):
self.root.ids.ti_box.remove_widget(w)
error message:- 
 Traceback (most recent call last):
   File "kivy\properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'tl'
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'
Thanks,
Raja

Elliot Garbus

unread,
Feb 22, 2021, 3:10:21 PM2/22/21
to kivy-...@googlegroups.com

You need to get to the root widget… app.root.ids.t1

 

Button:
                text:'Next'
                on_press:
                    print('Ready to run the process.')

                    app.root.ids.tl.getAll()

Raja Ramesh

unread,
Feb 23, 2021, 1:17:12 AM2/23/21
to kivy-...@googlegroups.com
Hi Elliot,

I have modified as below highlighted and the layout of the screen got changed as attached. My next step is to read all values of Button text and textinput text and store the values in a variable for next steps.
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: 100
Label:
text: 'Click a Button'
font_size: 20
size_hint_y: None
height: 40
ButtonBox:
        TextAndLabel:
id: tl
                    app.root.ids.tl.getAll()
            Button:
text:'Close'
on_press: app.stop()
BoxLayout:
'''  

Thanks,
Raja

Capture.PNG
raja_1_2202.zip

Elliot Garbus

unread,
Feb 23, 2021, 8:48:13 AM2/23/21
to kivy-...@googlegroups.com

In app do something like:

 

def get_all(self):
    top = (
'Top Item', self.root.ids.tl.ids.ti.text )
    rest = [(w.text, w.ids.ti.text)
for w in self.root.ids.ti_box.children]
   
print([top] + rest[::-1])

 

The item on top does not have a label so I called it Top Item.  This generates tuples of labels and the text values.

You want this in a level above the TextAndLabel object because it needs to see across all of them.  You could create a TextAndLabel method to return the tuple.

 

Sample output: [('Top Item', 'Hello'), ('A', 'aaa'), ('D', 'ddd'), ('F', 'ffffff')]

Raja Ramesh

unread,
Feb 23, 2021, 9:22:28 AM2/23/21
to kivy-...@googlegroups.com
Thank you Elliot...i will try and get back to you.

Raja Ramesh

unread,
Feb 25, 2021, 11:21:11 AM2/25/21
to kivy-...@googlegroups.com
Hi Elliot,

I am trying to copy an URL present in outlook mail body using win32com.cient in python. Do you have any idea how to copy URLs present in a mail?

Thanks,
Raja

Elliot Garbus

unread,
Feb 25, 2021, 12:17:22 PM2/25/21
to kivy-...@googlegroups.com

Assuming you have an unstructured email message with a url in the text,  I would use a regular expression to find the URLs.

See:

https://docs.python.org/3/howto/regex.html

https://docs.python.org/3/library/re.html

 

I like this tool when creating regex expressions: https://regex101.com/

If you do a google search for finding a url with regex, you will find a number of examples.

Raja Ramesh

unread,
Feb 25, 2021, 11:44:24 PM2/25/21
to kivy-...@googlegroups.com
Thank you Elliot....i will try and get back to you...

Reply all
Reply to author
Forward
0 new messages