Multiple recycleView are stacking - python

I've created an app with a main page that is going to have multiple sections with recycleView data. I have added the first but when I try to add a second (eventually adding 4 to replace the APPS, OTHER, etc sections) the second recycleView shows up on top of the first one (see top left of image) instead of next to it like I would expect. I tried putting both of the recycleView widgets inside a boxlayout but the second RV still overlays the top.
The information should be filling the second box on the right here instead of overlaying on the far top left
Screen:
MDNavigationLayout:
ScreenManager:
id: screenManager
Screen:
name: "monitor"
CoverImage:
source: 'images/monitor.png'
# Darken the photo
canvas:
Color:
rgba: 0, 0, 0, .6
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Administration'
left_action_items: [["menu", lambda x: nav_drawer.set_state('toggle')]]
elevation:5
BoxLayout:
ServerListWidget:
MountListWidget:
Please let me know if you need to see any additional code. I've also tried using a GridLayout with 2 cols
<MountListWidget>:
id: mountListWidget
recycleView: recycleView_mounts
BoxLayout:
padding: dp(20)
spacing: dp(10)
BoxLayout:
spacing: dp(10)
BoxLayout:
canvas.before:
Color:
rgba: 1,1,1,.1
# Use a float layout to round the corners
RoundedRectangle:
pos: self.pos
size: self.size
RecycleView:
id: recycleView_mounts
viewclass: 'MountWidget'
RecycleGridLayout:
cols: 1
default_size: self.parent.width, dp(60)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
width: self.minimum_width
spacing: dp(155), dp(0)
<ServerListWidget>:
id: serverListWidget
recycleView: recycleView
BoxLayout:
padding: dp(20)
spacing: dp(10)
BoxLayout:
orientation: "horizontal"
BoxLayout:
canvas.before:
Color:
rgba: 1,1,1,.1
# Use a float layout to round the corners
RoundedRectangle:
pos: self.pos
size: self.size
RecycleView:
id: recycleView
viewclass: 'ServerWidget'
RecycleGridLayout:
cols: 1
default_size: self.parent.width / 5, dp(60)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
width: self.minimum_width
spacing: dp(155), dp(0)
<CoverImage#CoverBehavior+Image>:
reference_size: self.texture_size
<MountWidget>:
BoxLayout:
#size_hint_max_x: dp(360)
size_hint_min_x: dp(150)
orientation: "horizontal"
BoxLayout:
orientation: "horizontal"
Button:
# Use the on_press to print troubleshooting info, otherwise comment out
on_press: print(self.background_color)
padding: dp(10), dp(10)
text_size: self.size
font_size: dp(18)
background_color: .5,1,1,.6
halign: "left"
valign: "top"
size: 1,1
text: str(root.name)
bold: True
color: (1,1,1)
<ServerWidget>:
BoxLayout:
#size_hint_max_x: dp(360)
size_hint_min_x: dp(150)
orientation: "horizontal"
BoxLayout:
orientation: "horizontal"
Button:
# Use the on_press to print troubleshooting info, otherwise comment out
on_press: print(self.background_color)
padding: dp(10), dp(10)
text_size: self.size
font_size: dp(22) if root.has_issue else dp(18)
background_color: .5,1,1,.6 #utils.get_random_color(alpha=.6)
halign: "left"
valign: "top"
size: 1,1
# Show last 4 of server name
text: str(root.name).upper()[-4:]
bold: True
color: utils.get_color_from_hex(warn) if root.has_issue else (1,1,1)
Button:
background_color: .5,1,1,.6
text_size: self.size
font_size: dp(22) if root.work >= 85 and root.work < 90 else dp(26) if root.work >= 90 else dp(18)
valign: "center"
halign: "center"
text: str(root.work)
padding: dp(8), dp(8)
bold: True if root.work > 90 else False
# yellow red white
color: (1,1,0) if root.work >= 85 and root.work < 90 else utils.get_color_from_hex(warn) if root.work >= 90 else (1,1,1)

Related

How do I align text inside a kivy label within a .kv file?

I try to make a gui for a text based RPG, and now I want to align the text of some labels to the top left, but "halign:" and "valign:" don't seem to do anything.
So how do I align the text inside my labels? Is there something I have done horribly wrong?
This is how the GUI looks at this moment and I marked where the text should be with green arrows:
This is how my .kv file looks:
BoxLayoutExample:
<BackgroundColor#Widget>:
background_color: 1,1,1,1
canvas.before:
Color:
rgba: root.background_color
Rectangle:
size: self.size
pos: self.pos
<BackgroundLabel#Label+BackgroundColor>:
background_color: 0, 0, 0, 0
<BoxLayoutExample>:
orientation: "vertical"
BoxLayout:
orientation:"horizontal"
BackgroundLabel:
background_color: 1,0,0,1
text: "Placeholder Text\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
halign: "left"
valign: "top"
font_size: "10sp"
size_hint: .5, 1
Label:
text: "Placeholder Map/Enemy #TEST TEST TEST TEST TEST \nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
halign: "left"
valign: "top"
size_hint: 1, 1.3
BoxLayout:
orientation:"vertical"
size_hint: .5, 1
Label:
background_color: 1,1,1,.5
text: "Placeholder Stats\nHP\nMP\nDMG\nXP\nLVL"
halign: "left"
valign: "top"
size_hint: 1, .3
ScrollView:
size_hint: 1, .7
scroll_distance: 100
Label:
text: "Placeholder Inventory\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
size_hint: None, None
size: self.texture_size
halign: "left"
valign: "top"
BoxLayout:
orientation: "horizontal"
size: "60dp","60dp"
size_hint: None,None
Label:
size: "60dp","60dp"
size_hint: None,None
Button:
text: "go\nnorth"
size: "60dp","60dp"
size_hint: None,None
Label:
size: "60dp","60dp"
size_hint: None,None
BoxLayout:
orientation: "horizontal"
size: "180dp","60dp"
#pos: "0dp","60dp"
size_hint: None,None
Button:
text: "go\nwest"
size: "60dp","60dp"
#pos: "0dp","60dp"
size_hint: None,None
pos_hint: {"x":0}
Button:
text: "go\nsouth"
size: "60dp","60dp"
size_hint: None,None
Button:
text: "go\neast"
size: "60dp","60dp"
#pos: "0dp","60dp"
size_hint: None,None
Label:
size: "60dp","60dp"
size_hint: None,None
Button:
text: "use\nitem"
size: "60dp","60dp"
size_hint: None,None
Button:
text: "equip\ngear"
size: "60dp","60dp"
size_hint: None,None
Button:
text: "unequip\ngear"
size: "60dp","60dp"
size_hint: None,None
Thanks for your help, I really appreciate it.
You need to add this argument in your label:
text_size: self.size
Then the halign and valign arguments will work properly, for example:
Label:
text: "Placeholder Map/Enemy #TEST TEST TEST TEST TEST \nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
text_size: self.size
halign: "left"
valign: "top"
size_hint: 1, 1.3
More details in the official kivy documentation.

Horizontal and vertical scrollview on same screen kivy

I'm trying to create a screen which has a horizontal scrollview at the top of the page, taking up ~25% of the vertical space and a vertical scrollview taking up the rest of the space (and then being able to scroll further down the screen).
I've managed to create a horizontal scrollview but can't get the vertical scrollview to work on the same screen.
py file:
import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.image import Image
from
Window.clearcolor = (1,1,1,1)
class Window(Screen):
pass
class MyApp(App):
theme_cls = ThemeManager()
def build(self):
kv = Builder.load_file("kivy.kv")
self.sm = WindowManager()
screens = [Window(name="window")]
for screen in screens:
self.sm.add_widget(screen)
self.sm.current = "window"
return self.sm
if __name__ == '__main__':
MyApp().run()
kv file:
<Window>:
name: "window"
NavigationLayout:
id: nav_layout
MyNavDrawer:
BoxLayout:
orientation: "vertical"
MDToolbar:
pos_hint: {'top': 1}
md_bg_color: 1, 1, 1, 1
ScrollView: #horizontal scrollview - works fine
size_hint_y: 0.5
GridLayout:
rows: 1
id: scroll_horizontal
size_hint_x: None
col_default_width: root.width*0.2
width: self.minimum_width*1.3
spacing: 20
ScrollView: #vertical scrollview, not working
size_hint_y: 0.85
do_scroll_x: False
GridLayout:
id: scroll_vertical
cols: 1
size_hint_y: None
spacing: 40
height: self.minimum_height
canvas:
Color:
rgba: (1, 1, 1, 1)
Rectangle:
size: self.size
pos: self.pos
BoxLayout:
cols: 2
spacing: 10
Button:
size_hint_y: 0.5
text: "Btn1"
Button:
size_hint_y: 0.5
text: "Btn2"
BoxLayout:
cols: 2
spacing: 10
Button:
size_hint_y: 0.5
text: "Btn1"
Button:
size_hint_y: 0.5
text: "Btn2"
BoxLayout:
cols: 2
spacing: 10
Button:
size_hint_y: 0.5
text: "Btn1"
Button:
size_hint_y: 0.5
text: "Btn2"
I'd like them to be independent of each other, so you can either scroll horizontal on the top scrollview or scroll veritcally through the buttons on the boxlayouts on the vertical scrollview
It took some work to get your MCVE working, but here is the part of your kv that I modified to get scrolling working in both directions with two ScrollViews:
ScrollView: #horizontal
size_hint_y: 0.25
do_scroll_y: False
GridLayout:
rows: 1
id: scroll_horizontal
size_hint: None, 1
col_default_width: root.width*0.2
width: self.minimum_width*1.3
spacing: 20
BoxLayout:
cols: 2
spacing: 10
size_hint_x: None
width: self.minimum_width
Button:
size_hint: None, 1
width: 500
text: "Btn1"
Button:
size_hint: None, 1
width: 500
text: "Btn2"
BoxLayout:
cols: 2
spacing: 10
size_hint_x: None
width: self.minimum_width
Button:
size_hint: None, 1
width: 500
text: "Btn1"
Button:
size_hint: None, 1
width: 500
text: "Btn2"
BoxLayout:
cols: 2
spacing: 10
size_hint_x: None
width: self.minimum_width
Button:
size_hint: None, 1
width: 500
text: "Btn1"
Button:
size_hint: None, 1
width: 500
text: "Btn2"
ScrollView: # vertical
size_hint_y: 0.75
do_scroll_x: False
GridLayout:
id: scroll_vertical
cols: 1
size_hint: 1.0, None
spacing: 40
# must set height
height: self.minimum_height
BoxLayout:
cols: 2
spacing: 10
size_hint_y: None
Button:
size_hint: 0.5, None
height: 100
text: "Btn1"
Button:
size_hint: 0.5, None
height: 100
text: "Btn2"
BoxLayout:
cols: 2
spacing: 10
size_hint_y: None
height: self.minimum_height
Button:
size_hint: 0.5, None
height: 100
text: "Btn1"
Button:
size_hint: 0.5, None
height: 100
text: "Btn2"
BoxLayout:
cols: 2
spacing: 10
size_hint_y: None
height: self.minimum_height
Button:
size_hint: 0.5, None
height: 100
text: "Btn1"
Button:
size_hint: 0.5, None
height: 100
text: "Btn2"

python/kivy : Make a Label Bold in kivy

When i pass blank value in array
arr = ({'Item1': ''},{'Item2': 1000})
then it show 0 instead of blank value.
1. Can someone tell me how to set blank by deafault instead of 0 ?
2. How to make key(Item1) bold?
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
def convert_data(data):
l = []
for item in data:
for key, value in item.items():
l.append({'text': key, 'value': value})
return l
class Invoice(Screen):
def abc(self):
#fetching from database
arr = ({'Item1': ''},{'Item2': 1000})
# convert to [{'text': 'Item1', 'value': 5000}, {'text': 'Item2', 'value': 1000}]
self.rv.data = convert_data(arr)
class MyApp(App):
def build(self):
return Builder.load_file('test.kv')
if __name__ == '__main__':
MyApp().run()
test.kv
<Button#Button>:
font_size: 15
size_hint_y:None
height: 30
<Label#Label>:
font_size: 15
size_hint_y:None
height: 30
<Item#GridLayout>:
cols: 2
text: ""
value: 0
padding : 5, 0
spacing: 10, 0
Label:
size_hint_x: .35
text: root.text
halign: 'left'
valign: 'middle'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Label:
size_hint_x: .15
text: str(root.value)
halign: 'right'
valign: 'middle'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Invoice:
rv: rv
BoxLayout:
orientation: "vertical"
padding : 15, 15
BoxLayout:
orientation: "vertical"
padding : 5, 5
size_hint: .6, None
pos_hint: {'x': .18,}
BoxLayout:
orientation: "horizontal"
padding : 5, 5
spacing: 10, 10
size: 800, 40
size_hint: 1, None
Button:
text: "Show"
size_hint_x: .05
spacing_x: 30
on_press:root.abc()
BoxLayout:
orientation: "horizontal"
size_hint: 1, 1
BoxLayout:
orientation: "vertical"
size_hint: .5, 1
padding : 0, 15
spacing: 10, 10
size: 500, 30
Button:
text: "Invoice"
text_size: self.size
halign: 'center'
valign: 'middle'
BoxLayout:
RecycleView:
id: rv
viewclass: 'Item'
RecycleBoxLayout:
default_size: None, dp(30)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
When I proposed this code in a previous question I assumed that the second fields were always going to be integers and not string, so create the value: 0 property which, as seen, accepts only numerical values since it is mapped as NumericProperty. The solution is to convert it to StringProperty by changing to value:"" besides changing the line of convert_data so that it does the conversion of integers to string.
In the second case to be able to use bold in the text we must set markup to True in Label and use [b] [/b] before and after the text that we want to modify.
All of the above implies the following changes:
*.py
[...]
def convert_data(data):
l = []
for item in data:
for key, value in item.items():
l.append({'text': key, 'value': str(value)}) # change this line
return l
[...]
*.kv
[...]
<Item#GridLayout>:
cols: 2
text: ""
value: ""
padding : 5, 0
spacing: 10, 0
Label:
size_hint_x: .35
text: "[b]{}[/b]".format(root.text)
markup: True
halign: 'left'
valign: 'middle'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Label:
size_hint_x: .15
text: root.value
halign: 'right'
valign: 'middle'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
[...]

Image Size on Kivy

Im creating a very simple aplication for pratice, and I´m having some trouble with Kivy GUI.
I would like to get all the images on the same size,and if it´s possible creat aline that separetes all the vertical box layouts.
:
name:'Prices'
BoxLayout:
orientation:'vertical'
canvas.before:
Rectangle:
source:'back_azul.png'
pos: self.pos
size: self.size
BoxLayout:
orientation:'horizontal'
height:'30dp'
size_hint_y:None
Button:
size_hint_x:0.25
text:"Back to Menu"
opacity: 1 if self.state == 'normal' else .5
background_color:0,0,0,0
on_release:app.root.current="main"
font_size:20
BoxLayout:
background_color:0,10,10,1
padding:5
Image:
source:"camisa.jpg"
Label:
text:"01 Camisa social"
bold:True
font_size:11
Label:
text:"R$: 8,00"
font_size:15
BoxLayout:
padding:5
Image:
source:"peca.jpg"
Label:
text:"01 Camisa Polo"
font_size:11
bold:True
Label:
text:"R$:6,00"
font_size:10
BoxLayout:
padding:5
Image:
source:"terno.jpg"
Label:
text:"01 Terno c/Calca"
font_size:11
bold:True
Label:
text:"R$: 28,00"
font_size:10
BoxLayout:
padding:5
Image:
source:"vestido.jpg"
Label:
text:"01 Vestido"
font_size:11
bold:True
Label:
text:"R$: 70,00"
font_size:10
Same width of the images:
Option 1: you can set the width, but must set the corresponding size hint to None.
Image:
size_hint_y: None
source:"Astronaut3.jpg"
width: 100
allow_stretch: True
Option2: use the size_hint
Image:
source:"Astronaut2.jpg"
size_hint_x: 0.4
allow_stretch: True
Creating a line
Again there are different options. You could use the Line from kivy Graphics. An easy and straightforward solution is to use a Label and make it your color of choice and then make it really small.
Label:
canvas.before:
Color:
rgba: (1,1,1,1)
Rectangle:
size: self.size
pos: self.pos
size_hint_y: None
height: 1
Sample App
Here are all the things mentioned in one sample App. It is not good practice to repeat yourself when coding, but I am doing it here to mirror your question as close as possible.
sample app example:
from kivy.app import App
from kivy.base import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
Builder.load_string("""
<rootwi>:
orientation:'vertical'
BoxLayout:
padding:5
Image:
source:"Astronaut2.jpg"
size_hint_x: 0.4
allow_stretch: True
Label:
text:"01 Camisa Polo"
font_size:11
bold:True
Label:
text:"R$:6,00"
font_size:10
Label:
canvas.before:
Color:
rgba: (1,1,1,1)
Rectangle:
size: self.size
pos: self.pos
size_hint_y: None
height: 1
BoxLayout:
padding:5
Image:
source:"Astronaut3.jpg"
size_hint_x: 0.4
allow_stretch: True
Label:
text:"01 Camisa Polo"
font_size:11
bold:True
Label:
text:"R$:6,00"
font_size:10
Label:
canvas.before:
Color:
rgba: (1,1,1,1)
Rectangle:
size: self.size
pos: self.pos
size_hint_y: None
height: 1
BoxLayout:
padding:5
Image:
size_hint_y: None
source:"Astronaut2.jpg"
width: 100
allow_stretch: True
Label:
text:"01 Camisa Polo"
font_size:11
bold:True
Label:
text:"R$:6,00"
font_size:10
Label:
canvas.before:
Color:
rgba: (1,1,1,1)
Rectangle:
size: self.size
pos: self.pos
size_hint_y: None
height: 1
BoxLayout:
padding:5
Image:
size_hint_y: None
source:"Astronaut3.jpg"
width: 100
allow_stretch: True
Label:
text:"01 Camisa Polo"
font_size:11
bold:True
Label:
text:"R$:6,00"
font_size:10
""")
class rootwi(BoxLayout):
pass
class MyApp(App):
def build(self):
return rootwi()
if __name__ == '__main__':
MyApp().run()

How to bind using kivy only?

I want to create a Widget changing only the text of a label inside it, but all the ways I find how to change this are by changing it in the python code instead of reusing only Kivy objects.
So I have a widget like the following:
<AmiLabel#Label>
color: .1, .5, .8, 1
font_size: 16
<AmiTextInput#TextInput>
font_size: 16
<PropertyInputForm>:
BoxLayout:
size: root.size
pos: root.pos
orientation: 'horizontal'
AmiLabel:
text: 'Folder Location'
size_hint_x: .5
AmiTextInput:
text: 'None'
size_hint_x: .5
<MainFormWidget>:
BoxLayout:
size: root.size
pos: root.pos
id: foo_bar
padding: 5
spacing: 5
canvas:
Color:
rgb: (1, 1, 1)
Rectangle:
pos: self.pos
size: self.size
orientation: 'vertical'
AmiLabel:
height: 36
size_hint_x: 1
size_hint_y: None
text: 'Project Name'
PropertyInputForm:
height: 36
size_hint_x: 1
size_hint_y: None
# I WANT TO CHANGE THE TEXT OF THE LABEL IN HERE
PropertyInputForm:
height: 36
size_hint_x: 1
size_hint_y: None
# I WANT TO CHANGE THE TEXT OF THE LABEL IN HERE
All I want to is change the text of the label from another widget in another level without touching the python code.
¿Is that possible?
One simple way would be to add a new property to your PropertyInputForm and reference or set that.
<PropertyInputForm>:
new_text_property: ''
BoxLayout:
size: root.size
pos: root.pos
orientation: 'horizontal'
AmiLabel:
text: root.new_text_property
size_hint_x: .5
AmiTextInput:
text: 'None'
size_hint_x: .5
and later
PropertyInputForm:
height: 36
size_hint_x: 1
size_hint_y: None
new_text_property: 'whatever'
You may also need to declare new_text_property in the python class to have it be a StringProperty rather than an ObjectProperty, though even that is I think not necessary in kivy 1.8.

Categories