RE: Dynamically Add TextInput Widgets into Gridlayout inside Scrollview

893 views
Skip to first unread message

Yowser MaGuil

unread,
Mar 6, 2014, 5:34:13 PM3/6/14
to kivy-...@googlegroups.com
I am trying to create an app that emulates google hangouts: a scrollable window that populates with text blobs.


In terms of Kivy's widgets, I am trying to dynamically add readOnly TextInput widgets to a GridLayout. This is inside a fixed viewport inside a ScrollView. 

#my python code is:-----------------------------------------------------------------------------------------------------------

import kivy

from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.graphics import *
from kivy.uix.scrollview import ScrollView 
from kivy.uix.listview import ListView
from kivy.properties import ObjectProperty


class ChatRoot(GridLayout):
def updateMessages(self):
pass

message_display_window = ObjectProperty()
message_input = ObjectProperty()

        #user types text, hits send, and a new widget should append inside the gridlayout
def sendMessage(self):
self.message_display_window.add_widget(TextInput(text=self.message_input.text, readonly="true", size_hint=(1,"None"), height=200))
self.message_display_window.height = self.minimum_height
print(self.message_input.text)


class ChatApp(App):
    pass

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


#my kivy language code is chat.kv:---------------------------------------------------------------------->

ChatRoot:
<ChatRoot@GridLayout>:
cols: 1
message_input: mess_in
message_display_window: mess_dis
GridLayout:
cols: 2
Button: 
size_hint: (1, 1)
text: "dude"
on_press: root.updateMessages()
GridLayout:
cols: 1
size_hint: (2, 1)
GridLayout:
cols: 1
size_hint: (1, 10)
ScrollView:
bar_pos_y: "right"
bar_width: 10
bar_margin: 1
scroll_type: ["bars"]
       do_scroll_y: True
       scroll_y: 0
     
       GridLayout:
        id: mess_dis
        cols: 1
        size_hint: (1, None)
       
        height: self.minimum_height
       
        TextInput:
                                               #for the sake of saving space in this post, the text below is much shorter than in my actual code. in practice, there will be many textInput widgets
        text: "blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n blah\n"
        size_hint: 1, None
                        readonly: True
       
   GridLayout:
    cols: 2
    size_hint: (2, 1)
   
    TextInput:
    id: mess_in
    size_hint: (8, 1)

    Button:
    size_hint: (1, 1)
    text: "Send"
    on_press: root.sendMessage()

#----------------------------------------------------

1) when I try to add a widget by hitting "send", I am getting the following error:

Traceback (most recent call last):
   File "newGui.py", line 40, in <module>
     ChatApp().run()
   File "/usr/lib/python3.3/site-packages/kivy/app.py", line 792, in run
     runTouchApp()
   File "/usr/lib/python3.3/site-packages/kivy/base.py", line 481, in runTouchApp
     EventLoop.window.mainloop()
   File "/usr/lib/python3.3/site-packages/kivy/core/window/window_pygame.py", line 381, in mainloop
     self._mainloop()
   File "/usr/lib/python3.3/site-packages/kivy/core/window/window_pygame.py", line 287, in _mainloop
     EventLoop.idle()
   File "/usr/lib/python3.3/site-packages/kivy/base.py", line 330, in idle
     Clock.tick_draw()
   File "/usr/lib/python3.3/site-packages/kivy/clock.py", line 429, in tick_draw
     self._process_events_before_frame()
   File "/usr/lib/python3.3/site-packages/kivy/clock.py", line 562, in _process_events_before_frame
     if event.tick(self._last_tick) is False:
   File "/usr/lib/python3.3/site-packages/kivy/clock.py", line 309, in tick
     ret = callback(self._dt)
   File "/usr/lib/python3.3/site-packages/kivy/uix/gridlayout.py", line 428, in do_layout
     strech_h * row_stretch / rows_weigth)
 ZeroDivisionError: float division by zero



What am I doing wrong?



2) It would be ideal to have the textInput widgets' heights be defined by the amount of text inside them. ie A single line of text would be bound by a textInput widget that is a single line high, plus padding

Is it possible to get the height of the text itself if the width is a constant (in this case, the width of the containing element, the Scrollview)?
Or define dynamic width and height, based on amount of text...(this is how Google hangouts does it).


Thanks!!!


qua-non non

unread,
Mar 7, 2014, 2:39:28 AM3/7/14
to kivy-...@googlegroups.com


--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Yowser MaGuil

unread,
Mar 7, 2014, 1:45:47 PM3/7/14
to kivy-...@googlegroups.com
@qua-non: Thanks! That totally works! 
[Using size_hint=(1,"None") AND height =self.minimum_height.]

Now I just need to get rid of this zeroDivisionError... >:{
Reply all
Reply to author
Forward
0 new messages