GridLayout Not Scrolling in Kivy - python

Please tell me why this doesn't work, the whole program works fine, it uses a function inside the main program to get it's text, but it won't scroll so the user won't be able to view the entire output.
<AnswerScreen#Screen>:
input_textb: input_textb
ScrollView:
size_hint: (1, None)
do_scroll_y: True
do_scroll_x: False
bar_width: 4
GridLayout:
padding: root.width * 0.02, root.height * 0.02
cols: 1
size_hint_y: None
size_hint_x: 1
height: self.minimum_height
Label:
id: input_textb
text: ""
font_size: root.height / 25
text_size: self.width, None
Edit:
I had already tried doing the same as many previous answers, in the particular one mentioned in the comments, I got an error saying "NoneType" has no attribute "bind".
I removed the size hint, it still doesn't work, but thanks anyway.
The text is definitely long enough.

I believe the label's size is not set, which i agree can be confusing at first, Label has a widget size (size as all widgets) and a texture_size, which is set to the actual size of the displayed text, kivy doesn't relate these two in any particular way at first, and it's up to you to decide how one influences the other, you did half of the work in setting text_size to (width, None), which forces the texture into having the width of the widget, but you are missing the other part of the deal, which is that you want to be the widget to be as tall as the generated texture. For this size to be effective, you also have to disable size_hint_y for Label, since it's in a GridLayout.
Label:
id: input_textb
text: ""
font_size: root.height / 25
text_size: self.width, None
height: self.texture_size[1]
size_hint_y: None
and you should be all set.

You should set a value for the property scroll_y of the ScrollView between 0 and 1.

Related

Kivy Recycle View Dynamic widget height causing lagging scrolling and jumping

So I am making an app that requires me to load a list of custom widgets(MDCards) into a RecycleView. Each MDcard will contain two labels and a checkbox. The text can be any size and thus the MDCard has to change its height based on the text that it contains. Here is the part in the kv file that deals with this...NOTE: the text i am adding now is temporary to allow me to create different cards with progressively larger heights.
Custom MDCARD Widget
<IndivualReminderElementBlueprint#MDCard>:
md_bg_color:218/255,68/255,83/255,1
size_hint: 1, None
text: ''
Label:
on_size: root.height = self.height
size_hint: 1, None
text_size: root.width, None
size: self.texture_size
text: root.text
The kv code for the RecycleView class
<ListViewBlueprint>:
viewclass: 'IndivualReminderElementBlueprint'
data: [{"text": f"{i} {'test'*i**2}"} for i in range(20)]
RecycleBoxLayout:
id: container
default_size: None, None
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
spacing:'20dp'
I am aware that this jerkiness in the scrolling is caused because the size of the widgets is calculated as the user scrolls down which also causes the scroll position to jump randomly. I also looked into this issue posted on Github https://github.com/kivy/kivy/issues/6582 . But i am unable to understand the code at the end or if it will actually fix this problem? I was also wondering if there was any other way to fix this problem? Thanks :)

Adjust Kivy FileChooser Font Size

I'm looking for how I can change the font size of all text in a kivy FileChooserListView class. I am hoping to do so in pure python if possible. I like that the standard Label and Button uix classes take font_size as an initialize parameter, but it appears the FileChooserListView does not. If doing so in pure python isn't an easy approach and there's a good .kv file approach, I'd take that too. Thanks!
There is no direct way to do that, but you can fiddle with the FileListEntry template that the FileChooserListView uses to display the entries. In that template, you can adjust the font sizes used. Here is my attempt at that:
Builder.load_string('''
[FileListEntry#FloatLayout+TreeViewNode]:
locked: False
entries: []
path: ctx.path
# FIXME: is_selected is actually a read_only treeview property. In this
# case, however, we're doing this because treeview only has single-selection
# hardcoded in it. The fix to this would be to update treeview to allow
# multiple selection.
is_selected: self.path in ctx.controller().selection
orientation: 'horizontal'
size_hint_y: None
height: '96dp' if dp(1) > 1 else '48dp' # height must be big enough to hold font sized below
# Don't allow expansion of the ../ node
is_leaf: not ctx.isdir or ctx.name.endswith('..' + ctx.sep) or self.locked
on_touch_down: self.collide_point(*args[1].pos) and ctx.controller().entry_touched(self, args[1])
on_touch_up: self.collide_point(*args[1].pos) and ctx.controller().entry_released(self, args[1])
BoxLayout:
pos: root.pos
size_hint_x: None
width: root.width - dp(10)
Label:
id: filename
font_size: '48dp' # adjust this font size
size_hint_x: None
width: root.width - sz.width # this allows filename Label to fill width less size Label
text_size: self.width, None
halign: 'left'
shorten: True
text: ctx.name
Label:
id: sz
font_size: '48dp' # adjust this font size
#text_size: self.width, None
size_hint_x: None
width: self.texture_size[0] # this makes the size Label to minimum width
text: '{}'.format(ctx.get_nice_size())
''')
This is based heavily on the FileListEntry template in style.kv. There are two font_sizes that you can adjust, and a height for the entry. All three must be coordinated to get a reasonable result.

Multiple valigns in one label

I am programming a Chat Bot and I want to build a GUI using kivy. To make the chat I am using labels in a scrollview:
GridLayout:
cols: 1
rows: 0
ScrollView:
size: self.size
do_scroll_x: False
Label:
id: msg
text_size: self.width,None
size_hint_y: None
height: self.texture_size[1]
font_size: 20
Python Code:
def send(self,x):
#global msgback
self.msg_list.text += str(x + "\n")
The problem is, that I do not know how to make the valign, that only makes the messages from the user on the right side. How do I do that?
Use halign to align text to the right.
Label:
id: msg
text_size: self.width,None
size_hint_y: None
height: self.texture_size[1]
font_size: 20
halign: 'right'
valign: 'middle'
Label » halign
halign
Horizontal alignment of the text.
halign is an OptionProperty and defaults to ‘left’. Available options
are : left, center, right and justify.
Warning
This doesn’t change the position of the text texture of the Label
(centered), only the position of the text in this texture. You
probably want to bind the size of the Label to the texture_size or set
a text_size.

How do you compacted a GridLayout vertical space in Kivy?

Have a look at the screenshot below:
I kinda new in this Kivy layout scheme, previously I work a lot in PyQt. In PyQt, that blank vertical space can easily get rid of by using Spacer. But how do you do this in Kivy? Below is part of the KV file that constitute this layout.
GridLayout:
cols: 1
GridLayout:
cols: 2
row_default_height: '48dp'
row_force_default: True
spacing: 10, 10
padding: 10, 10
Label:
size_hint: None, None
text: 'Input'
halign: 'left'
valign: 'top'
text_size: self.size
width: 50
TextInput:
id: txt_url
size_hint: 1, None
text: ''
TabbedPanel:
id: tp
do_default_tab: False
TabbedPanelItem:
id: tab_fl
text: ''
TabbedPanelItem:
text: ''
FloatLayout
id: box
TabbedPanelItem:
text: ''
FloatLayout
id: box
I Would love to know what is the best practice of using Kivy layout mechanism. :)
In this case, you can take advantage of the GridLayout's minimum_height property to size it appropriately.
GridLayout:
cols: 1
GridLayout:
cols: 2
row_default_height: '48dp'
row_force_default: True
spacing: 10, 10
padding: 10, 10
# add these two lines
size_hint_y: None
height: self.minimum_height
...
Because you're using one column width in your root GridLayout, it be possible to resize the rows based off children height using the following trick for setting rows_minimum.
GridLayout:
cols: 1
row_force_default: True
## Call it what ya like, just interested in setting 'self.rows_minimum'
foo: [self.rows_minimum.update({i: x.height}) for i, x in enumerate(reversed(list(self.children)))]
## ... rest of layout ...
The internal GridLayout and TabbedPanel are the rows/children of your root GridLayout, so setting heights for the internal elements should allow for the foo generator redirection to pull-up the excess space.
Now for readers that may have more than one column there be a more extensive example, that shows one way of handling auto-sizing of grid layouts and a bunch of other goodies, over at another answer regarding text input word wrapping.

How to center text vertically inside a text input in kv file?

I'm trying to center the text of a TextInput vertically in Kivy.
But no solution yet.
How can I do a valign for text input in the kv file? Also centering horizontally would be great to know, how to do it.
For labels I have checked the text align example from Kivy and there the alginment is working because there you can use 'valign' and 'halign' to do the alignment, but that's not available for TextInputs.
Maybe a trick with texture_size could help, but I need to check how this works. I have seen such a trick for a label, but I don't know if it works for the TextInput.
Here's my kv code that I have right now:
#: set Buttonheight1 40
BoxLayout:
size_hint_y: None
height: Buttonheight1
Label:
id: _number_label
text: "Number:"
font_size: 10
size_hint_x: None
width: 50
canvas.after:
Color:
rgba: 1,0,0,.5
Rectangle:
pos: self.pos
size: self.size
TextInput:
multiline: False
size_hint_y: None
height: _number_label.height
#padding_top: 10
font_size: 10
text: str(self.font_size)
#text: '%s, %s' % (self.get_center_x(), self.get_center_y()) #position test
Explanation of the kv code:
Buttonheight1 is a constant with kv set
Canvas.after is just for debugging the size of the label
The text of the text input shows font size as dummy text
Maybe it's simple to fix but I'm pretty new to Kivy and haven't found an example for this.
Here is how it looks like at the moment:
(Note: The OK button in the screenshot is not in the kv code above)
Looking at the api, all I could suggest is that you could try using padding, since you can specify:
Padding of the text: [padding_left, padding_top, padding_right, padding_bottom].
padding also accepts a two argument form [padding_horizontal, padding_vertical] and a one argument form [padding].
Maybe usingpadding_top and/or padding_bottom for instance you could center the text vertically.
As already suggested in the comments by AWolf.
This seems to work best:
padding: [0, (self.height-self.line_height)/2]
What worked for me was using pos_hint as follows:
TextInput:
multiline: False
size_hint_y: None
height: _number_label.height
font_size: 10
text: str(self.font_size)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
I know this is old post .. I just wanna put the answer if anyone search for this again.
Here is the solution:
In the kivy file add this line in the TextInput Properties
padding : 6,self.height/2 - self.font_size/2,6,6
I used this code in my custom text input.
As you see it works fine:

Categories