I have managed to get the size of a box layout to change based on the corresponding Label height, however, it doesn’t line up perfectly and I cannot figure out why.
I have looked at adding offsets but have had no luck since it ended up making the issue worse
Thank you for any help
JavaScript
x
111
111
1
MainInterface:
2
3
<MainInterface@BoxLayout>:
4
orientation: "vertical"
5
Label:
6
#font_name: "Nunito-Black.ttf"
7
text: "T R U T H"
8
size_hint: 1, 0.1
9
GridLayout:
10
size_hint: 1, 0.12
11
cols: 4
12
Button:
13
text: "Menu1"
14
Button:
15
text: "Menu2"
16
Button:
17
text: "Menu3"
18
Button:
19
text: "Menu4"
20
PageLayout:
21
border: "20dp"
22
swipe_threshold: 0.2
23
RecycleView:
24
viewclass: 'PostGrid'
25
scroll_y: 1
26
id: rv
27
data: app.posts
28
RecycleBoxLayout:
29
id: box
30
default_size: None, None
31
default_size_hint: 1, None
32
size_hint_y: None
33
padding: ["10dp", "16dp"]
34
spacing: "8dp"
35
height: self.minimum_height
36
orientation: 'vertical'
37
key_size: '_size'
38
BoxLayout:
39
orientation: "vertical"
40
Button:
41
text: "peni"
42
Button:
43
text: "tag @ will J"
44
Button:
45
text: "Input"
46
47
48
<PostGrid@BoxLayout>:
49
message_id: -1
50
orientation: "horizontal"
51
text: ''
52
spacing: "10dp"
53
#size_hint: self.width, None
54
_size: 0, 74
55
size: 0, 74
56
text_size: None, None
57
BoxLayout:
58
orientation: "vertical"
59
spacing: "10dp"
60
size_hint: .1, 1
61
size: self.size
62
Button:
63
text: "UP"
64
font_size: "5dp"
65
size_hint: 1, 0.2
66
Button:
67
text: "DOWN"
68
font_size: "5dp"
69
size_hint: 1, 0.2
70
Label:
71
text: "test"
72
font_size: "5dp"
73
size_hint: 1, 0.6
74
Label:
75
text: root.text
76
padding: 5, 5
77
size_hint: .9, 1
78
#size: self.texture_size
79
height: self.texture_size[1]
80
text_size: self.width, None
81
color: 0,0,0,1
82
#text_size: self.width, None
83
#size_hint: None, 1
84
#size: self.texture_size
85
#font_name: "Nunito-Bold.ttf"
86
#font_size: "12dp"
87
multiline: True
88
#size: 1, root.min_height
89
90
on_texture_size:
91
app.update_message_size(
92
root.message_id,
93
self.texture_size,
94
root.width)
95
96
pos: self.pos
97
98
canvas.before:
99
Color:
100
rgba: (0.8, 0.8, 0.8, 1)
101
RoundedRectangle:
102
size: self.texture_size
103
radius: [5, 5, 5, 5]
104
pos: self.x, self.y
105
canvas:
106
Color:
107
rgba:0,0.9,0.9,1
108
Line:
109
width:0.8
110
rounded_rectangle:(self.x,self.y,self.width,self.height, 5)
111
JavaScript
1
19
19
1
from kivy.app import App
2
from kivy.lang import Builder
3
from kivy.clock import Clock
4
from kivy.properties import ListProperty
5
from kivy.animation import Animation
6
from kivy.metrics import dp
7
8
class TruthApp(App):
9
posts = ListProperty([{'message_id':0, 'text':"testskjhfjksdhfkjshfjshfjkhskjdfhskjdhfkjshfdkjhsjkdfhskjhdfkjshdfjkhzxczxczxczxcxzczxcxsjkdfhjksdhfkjshkjdfhksjdhfjkshdfjkhsdkjhfkjshdfjkshkjfhskjhfkjshfjkshdkjfhskjhfjshfkjshdfjkshdjkfhskjhfkjshfjksdhjfhsjkdhfjkhsdkjfhskjhfjkngdgdgdndgdgdgndgdgdgndggdgd",'_size':[0,0] }, {'message_id':1, 'text':"testskjhfjksdhfkjshfjshfjkhskjdfhskjdhfkjshfdkjhsjkdfhskjhjfhskjhfjk,'_size':[0,0]"}])
10
11
def update_message_size(self, message_id, texture_size, max_width):
12
#print(self.posts)
13
#print("Here")
14
self.posts[message_id] = {**self.posts[message_id], '_size':[0, texture_size[1]]}
15
16
17
if __name__ == '__main__':
18
TruthApp().run()
19
^ Image showing how the code above runs
Advertisement
Answer
The problem is in the viewclass
of RecycleView
(i.e. PostGrid
). You set its height
as such it could not accomodate its children which is supposed to be the minimum height that will place all its children.
Now that’s exactly the attr. minimum_height
does. With this being applied you also don’t need default_size: None, None
(especially the height attr.)
With this and other modifications your .kv
file should now look like,
JavaScript
1
110
110
1
MainInterface:
2
3
<MainInterface@BoxLayout>:
4
orientation: "vertical"
5
Label:
6
#font_name: "Nunito-Black.ttf"
7
text: "T R U T H"
8
size_hint: 1, 0.1
9
GridLayout:
10
size_hint: 1, 0.12
11
cols: 4
12
Button:
13
text: "Menu1"
14
Button:
15
text: "Menu2"
16
Button:
17
text: "Menu3"
18
Button:
19
text: "Menu4"
20
PageLayout:
21
border: "20dp"
22
swipe_threshold: 0.2
23
RecycleView:
24
viewclass: 'PostGrid'
25
scroll_y: 1
26
id: rv
27
data: app.posts
28
RecycleBoxLayout:
29
id: box
30
default_size_hint: 1, None
31
size_hint_y: None
32
padding: ["10dp", "16dp"]
33
spacing: "8dp"
34
height: self.minimum_height
35
orientation: 'vertical'
36
key_size: '_size'
37
BoxLayout:
38
orientation: "vertical"
39
Button:
40
text: "peni"
41
Button:
42
text: "tag @ will J"
43
Button:
44
text: "Input"
45
46
47
<PostGrid@BoxLayout>:
48
message_id: -1
49
orientation: "horizontal"
50
text: ''
51
spacing: "10dp"
52
size_hint_y: None
53
#_size: 0, 74
54
height: self.minimum_height
55
text_size: None, None
56
BoxLayout:
57
orientation: "vertical"
58
spacing: "10dp"
59
size_hint: .1, 1
60
Button:
61
text: "UP"
62
font_size: "5dp"
63
size_hint: 1, 0.2
64
Button:
65
text: "DOWN"
66
font_size: "5dp"
67
size_hint: 1, 0.2
68
Label:
69
text: "test"
70
font_size: "5dp"
71
size_hint: 1, 0.6
72
Label:
73
text: root.text
74
padding: 5, 5
75
#size_hint: .9, 1
76
#size: self.texture_size
77
size_hint_y: None
78
height: self.texture_size[1]
79
text_size: self.width, None
80
color: 0,0,0,1
81
#text_size: self.width, None
82
#size_hint: None, 1
83
#size: self.texture_size
84
#font_name: "Nunito-Bold.ttf"
85
#font_size: "12dp"
86
multiline: True
87
#size: 1, root.min_height
88
89
on_texture_size:
90
app.update_message_size(
91
root.message_id,
92
self.texture_size,
93
root.width)
94
95
pos: self.pos
96
97
canvas.before:
98
Color:
99
rgba: (0.8, 0.8, 0.8, 1)
100
RoundedRectangle:
101
size: self.texture_size
102
radius: [5, 5, 5, 5]
103
pos: self.x, self.y
104
canvas:
105
Color:
106
rgba:0,0.9,0.9,1
107
Line:
108
width:0.8
109
rounded_rectangle:(self.x,self.y,self.width,self.height, 5)
110