from kivy.app import AppThis is actually taken from the documentation, http://kivy.org/docs/api-kivy.uix.scrollview.html#managing-the-content-size
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
class SMApp(App):
def build(self):
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
#Make sure the height is such that there is something to scroll.
layout.bind(minimum_height=layout.setter('height'))
for i in range(30):
btn = Label(text=str(i), size_hint_y=None, height=40)
layout.add_widget(btn)
root = ScrollView(size_hint=(None, None), size=(400, 400),
pos_hint={'center_x':.5, 'center_y':.5})
root.add_widget(layout)
return root
if __name__ == '__main__':
SMApp().run()
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
# Make sure the height is such that there is something to scroll.
layout.bind(minimum_height=layout.setter('height'))
for i in range(100):
btn = Button(text=str(i), size_hint_y=None, height=40)
layout.add_widget(btn)
root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
root.add_widget(layout)
runTouchApp(root)
from kivy.uix.scrollview import ScrollViewfrom kivy.uix.gridlayout import GridLayout
class Contenedor(GridLayout): _bind = True def __init__(self, *args, **kwargs): kwargs['size_hint_y'] = None kwargs['cols'] = 1 super(Contenedor, self).__init__(*args, **kwargs) def add_widget(self, widget, index=0): if self._bind: self.bind(minimum_height=self.setter('height')) self._bind = False super(Contenedor, self).add_widget(widget, index) self.height += widget.height def del_widget(self, widget): super(Contenedor, self).del_widget(widget) self.height -= wigdet.height class Container(ScrollView): def __init__(self, *args, **kwargs): kwargs['do_scroll_x'] = False super(Container, self).__init__(*args, **kwargs) self._grid_widget = Contenedor() super(Container, self).add_widget(self._grid_widget) @property def childs(self): return self._grid_widget.children def add_widget(self, widget, index=0): self._grid_widget.add_widget(widget, index=0) def del_widget(self, widget): self._grid_widget.del_widget(widget)
What exactly are your trying todo?
Do you want one long Label that can scroll horizontally?
You mention 4000 words. How are those arranged how do you want them displayed?
--
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/dbfd99e9-da97-4558-95ac-643d05e2a26bn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/63af87eb.050a0220.12ec7.db01SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAMsm4MkUSRC34NMo%3DEH7Tmck-oivV-XexTbWkb_FCJhi_9zosg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/ADECFF69-CE79-41DE-9640-2072FCE252BD%40cox.net.
One Label of 4000 words will exceed the max texture size, and will not work.
You need a mechanism to break the 4000 words up into smaller pieces, and assign each to a label. Depending on the number of words per Label, you may also want to use a RecycleView, rather than a ScrollView.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAMsm4Mkb5aG%3D7UB0Jfuxz9tHDVXos4BTb5NE1hcW7o%2B8i0X0RA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/63b04a86.170a0220.e36d1.de7eSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Here is a ScrollView, If this is the kind of thing your looking for I’ll do the Recycleview version.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.clock import Clock
kv = """
<ScrollLabel>:
size_hint: None, None
size: self.texture_size[0], dp(30)
BoxLayout:
orientation: 'vertical'
Label:
text: 'Test long Scroll'
AnchorLayout:
ScrollView:
BoxLayout:
id: scroll_box
size_hint: None, None
size: self.minimum_width, dp(30)
"""
class ScrollLabel(Label):
pass
class LongScrollApp(App):
def build(self):
return Builder.load_string(kv)
def on_start(self):
Clock.schedule_once(self.add_text, .5)
def add_text(self, _):
long_text = 'This is a very long piece of text. ' * 4000
scroll_box = self.root.ids.scroll_box
for substring in long_text.split('.'):
scroll_box.add_widget(ScrollLabel(text=substring + '.'))
LongScrollApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAMsm4MmWD%2BqmN1tv3MTGN5XP0WKpuP-FwqzA2Cf55Q%2BFEi0vMQ%40mail.gmail.com.
Here is a version with RecycleView, the ‘trick’ here is to use key_size and calculate the size of the text.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.properties import ListProperty
from kivy.clock import Clock
kv =
"""
<ScrollLabel>:
size_hint: None, None
BoxLayout:
orientation: 'vertical'
Label:
text: 'Test long Scroll'
AnchorLayout:
RecycleView:
id: rv
data: app.text
viewclass: 'ScrollLabel'
RecycleBoxLayout:
id: scroll_box
key_size: 'ks'
default_size_hint: None, None
size_hint: None, None
size: self.minimum_width, dp(30)
"""
class ScrollLabel(Label):
pass
class LongScrollApp(App):
text = ListProperty()
def build(self):
return Builder.load_string(kv)
def on_start(self):
Clock.schedule_once(self.add_text, .5)
def add_text(self, _):
long_text = 'This is a very long piece of text. ' * 400
for ss in long_text.split('.'):
t = ss + '.'
sl = ScrollLabel(text=t)
sl.texture_update()
self.text.append({'text': t, 'ks': sl.texture_size})
LongScrollApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/63b05493.050a0220.fd336.b650SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Show me a picture of how you want it to look. In your previous descriptions, you only mentioned horizontal scrolling of one line.
You can change the orientation of the RecycleBoxLayout to ‘vertical’, and make the other required adjustments…
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/b734f33b-7117-4388-b90c-c0fac8f8ad07n%40googlegroups.com.
Any characteristics of the text? Is it a collection of separate messages or just one long 4000 word message?
So you want to have the text fill the screen horizontally, wrap and then scroll vertically. Correct?
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3dd4ed9a-0b27-47fd-aab1-e3fcec62a345n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/63b06f45.250a0220.1b3c9.7cbbSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
If the text is just 4000 words, with no additional formatting, the trick will be to decide how to break up the text into smaller pieces.
Another option you could try is just using the Rst widget. https://kivy.org/doc/stable/api-kivy.uix.rst.html?highlight=rst#module-kivy.uix.rst
Let me know if the RST widget works for you.
If not I’ll see if I can come up with a flexible way to breaking the text up into “sections”. Are their line breaks in the text?
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAMsm4MmSQutV9vnU%3Dc1-iu8REDBT9P-5z9KPK1vLqQg9tfF6mw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/63b07e39.1f0a0220.e590c.cf61SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
It looks like this example from the kivy garden does what you want. Reading the issues – the code out of data and will need some updating but I’ expect there are some ideas here: https://github.com/kivy-garden/garden.scrolllabel
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAMsm4MnMXWc3H0U5ngZ0d0gTO4yfCm9K5jLswLXpuSUxGyxh-Q%40mail.gmail.com.
…. the code is out of date and will need some updating…
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/63b09837.1f0a0220.88c57.2d51SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/63b0992b.1f0a0220.8b477.a23bSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
You do not need to do an install, you can just copy the code from GitHub. As I mentioned it will need to be edited/fixed – there are some issues with the code.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/e9b74b91-1a12-4ae0-a5dc-0317adf5fe03n%40googlegroups.com.
RstDocument is derived from ScrollView. You can bind the keyboard and modify scroll_y, to cause the text to scroll. https://kivy.org/doc/stable/api-kivy.uix.scrollview.html#kivy.uix.scrollview.ScrollView.scroll_y
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAMsm4MmjaNLXCiF8YD17UGbE4so-xzt-MgaX7O%2BpM341tLEtqw%40mail.gmail.com.
You can go directly to the GitHub of ScrollLabel, and download the code as a zip file.
I’ll do an rst example.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/cbd34530-b206-4e3d-bb5e-7db6caadd27an%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/63b18a4a.630a0220.8422c.f0aaSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Here you go… I left the print statements in there so you can see how the keyboard works. The keyboard code is from the docs, see the link in the comment.
I selected 15 as the value in pixels to move on the up/down… you can change this if the font size is changed.
The document responds to the following keys: up, down, pageup, pagedown, home and end.
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.rst import RstDocument
from kivy.metrics import sp
kv = """
BoxLayout:
Label:
text: 'Scroll Rst with up/down keys'
KeyRstDocument:
text: ' '.join([f'Line {i} This is a long repeating text, it repeats over and over.' for i in range(150)])
"""
# see: https://kivy.org/doc/stable/api-kivy.core.window.html?highlight=window#kivy.core.window.Keyboard
class KeyRstDocument(RstDocument):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._keyboard = Window.request_keyboard(
self._keyboard_closed, self, 'text')
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def _keyboard_closed(self):
print('My keyboard have been closed!')
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
print('The key', keycode, 'have been pressed')
print(' - text is %r' % text)
print(' - modifiers are %r' % modifiers)
# Keycode is composed of an integer + a string
if keycode[1] in ['up', 'down', 'pageup', 'pagedown', 'home', 'end']:
self.process_key(keycode[1]) # your code to process the keyboard
# If we hit escape, release the keyboard
elif keycode[1] == 'escape':
keyboard.release()
# Return True to accept the key. Otherwise, it will be used by
# the system.
return True
def process_key(self, key):
if key == 'home':
self.scroll_y = 1
elif key == 'end':
self.scroll_y = 0
elif key == 'pageup':
scroll_distance = self.convert_distance_to_scroll(0, self.height)[1]
self.scroll_y = min(1, self.scroll_y + scroll_distance)
elif key == 'pagedown':
scroll_distance = self.convert_distance_to_scroll(0, self.height)[1]
self.scroll_y = max(0, self.scroll_y - scroll_distance )
elif key == 'up':
# selected 15 as about right for a line based on font size
scroll_distance = self.convert_distance_to_scroll(0, sp(15))[1]
self.scroll_y = min(1, self.scroll_y + scroll_distance)
elif key == 'down':
scroll_distance = self.convert_distance_to_scroll(0, sp(15))[1]
self.scroll_y = max(0, self.scroll_y - scroll_distance)
class ScrollRstApp(App):
def build(self):
return Builder.load_string(kv)
ScrollRstApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAMsm4Mm%3Dr0tG9vds_TvfVtTP2%3DqHDkcOKFDY10XTGvBkP1pB9A%40mail.gmail.com.
Added a small optimization, removing unnecessary creation of Labels in the add_text loop:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.recycleview import RecycleView
kv =
"""
<ScrollLabel>:
size_hint: 1, None
text_size: self.width, None # height must be calculated based on texture_size
font_size: 30
<ConsoleRV>:
viewclass: 'ScrollLabel'
RecycleBoxLayout:
id: scroll_box
orientation: 'vertical'
key_size: 'ks'
default_size_hint: 1, None
size_hint: 1, None
height: self.minimum_height
on_width: root.update_text() # when width changes, update sizes in rv data list
BoxLayout:
orientation: 'vertical'
Label:
text: 'Test long Scroll'
size_hint_y: None
height: 30
ConsoleRV:
id: console_rv
"""
class ScrollLabel(Label):
pass
class ConsoleRV(RecycleView):
def add_text(self, text):
st = [x + '\n' for x in text.split('\n')]
# assuming there are some \n in the text, split into separate labels
sl = ScrollLabel() # create a label to use for calculating texture size
for t in st:
sl.text = t
sl.width = self.width
sl.texture_update()
self.data.append({'text': t, 'ks': sl.texture_size})
def update_text(self):
sl = ScrollLabel()
for i, entry in enumerate(self.data):
sl.text = entry['text']
sl.width = self.width
sl.texture_update()
self.data[i]['ks'] = sl.texture_size
self.refresh_from_data()
sample_text = \
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/595ed905-e853-4b0b-bd19-2ecd13874e85n%40googlegroups.com.