Canvas rectangles not staying with their widgets

23 views
Skip to first unread message

Dusty

unread,
Dec 17, 2019, 6:25:14 PM12/17/19
to Kivy users support
Some canvases are where they should be (seemingly), while others overlay the previous item in the GridView.  In this example, I'd expect all text to be the same color, and the top part to be one solid grey block.  Can anyone tell me what I'm doing wrong?

Screenshot from 2019-12-17 17-20-37.png


Minimal.py
from kivy.app import App

from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.gridlayout import GridLayout

from kivy.uix.label import Label
from kivy.properties import BooleanProperty

from kivy.lang.builder import Builder
Builder.load_file('minimal.kv')


class Scroll(ScrollView):
   
pass


class Waypoints(GridLayout):
   
pass


class Root(FloatLayout):
   
pass


class Waypoint(RelativeLayout):
    active
= BooleanProperty()


class ServerApp(App):
   
def build(self):
       
super().build()

   
def add_waypoint(self):
        wp
= Waypoint()
        label
= Label(text="hey", font_size='40sp')
        wp
.add_widget(label)
       
self.waypoints.add_widget(wp)

   
def initialize_kivy(self):
       
self.wps = []
       
# Base layout
       
self.root = Root()
       
# Waypoints
       
self.scroll = Scroll()
       
self.root.add_widget(self.scroll)
       
self.waypoints = Waypoints()
       
self.scroll.add_widget(self.waypoints)
       
self.add_waypoint()
       
self.add_waypoint()
       
self.add_waypoint()
       
self.add_waypoint()
       
self.add_waypoint()
       
self.add_waypoint()

if __name__ == "__main__":
    app
= ServerApp()
    app
.initialize_kivy()
    app
.run()



minimal.kv
<Scroll>:
    pos_hint: {"right": 1, "top": 1}
    size_hint: (1, 1)
    canvas.before:
        Color:
            rgba: (0, 0, 0, 1)
        Rectangle:
            size: self.size
            pos: self.pos

<Waypoints>:
    cols: 1
    size_hint: (1, None)
    height: self.minimum_height
    padding: 0, 0
    spacing: 0, 0
    canvas.before:
        Color:
            rgba: (1, 1, 1, .2)
        Rectangle:
            size: self.size
            pos: self.pos

<Waypoint>:
    size_hint: (1, None)
    height: 50
    canvas.before:
        Color:
            rgba: (0, 0, 0, .5) # if self.active else (1, .0, .0, .05)
        Rectangle:
            size: self.size
            pos: self.pos


Dusty

unread,
Dec 17, 2019, 6:51:43 PM12/17/19
to Kivy users support
I made another screenshot, with Waypoint's background as transparent red, to make the problem more clearly visible.  Also, I think that the ones that are overlaid also have red behind them, so, they've got their own canvas and the next Waypoint's.

Screenshot from 2019-12-17 17-49-30.png



Dusty

unread,
Dec 17, 2019, 6:54:24 PM12/17/19
to Kivy users support
I'm using Kivy 1.11.1, Python 3.7, and Ubuntu 18.04.

Dusty

unread,
Dec 17, 2019, 9:56:16 PM12/17/19
to Kivy users support
I found that changing Waypoint's canvas.before to canvas.after fixed it.  I don't understand why, though.

Dusty

unread,
Dec 17, 2019, 10:00:05 PM12/17/19
to Kivy users support
Actually, I see now that it only partway fixed it.  It's in the correct place now, but it's overlaying the content.

Elliot Garbus

unread,
Dec 17, 2019, 10:03:11 PM12/17/19
to kivy-...@googlegroups.com

I don’t know the answer…

 

There is a discussion in the docs on RelativeLayouts and some common pitfalls: https://kivy.org/doc/stable/api-kivy.uix.relativelayout.html?highlight=relativelayout#module-kivy.uix.relativelayout

 

I don’t know if this is applicable…

--
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/4390565f-800e-4ec8-9de8-438ea99343d2%40googlegroups.com.

 

Dusty

unread,
Dec 17, 2019, 10:05:34 PM12/17/19
to Kivy users support
It does seem relevant:

As all positions within a RelativeLayout are relative to the position of the layout itself, the position of the layout should never be used in determining the position of sub-widgets or the layout’s canvas.

Robert Flatt

unread,
Dec 18, 2019, 1:10:20 AM12/18/19
to Kivy users support
There is something unusual about this App, I don't know if it is relevant. But  it might generate unexpected behavior:
The widget tree is built before the build call:

In its minimal form:
from kivy.app import App
from kivy.uix.label import Label

class ServerApp(App):
   
def build(self):
       
print('really build widgets')
       
return Label(text='blah')


if __name__ == "__main__":
    app
= ServerApp()

   
print('build widgets')    
    app
.run()

prints:
build widgets
really build widgets

It might be irrelevant, but it is unusual.

Dusty

unread,
Dec 18, 2019, 2:13:03 AM12/18/19
to Kivy users support
I gave up on setting a background.  I need the RelativeLayout, I think, and the page that Elliot Garbus linked shows that it can't be used in this way.  It's no real problem to use a different visual indicator.


On Wednesday, December 18, 2019 at 12:10:20 AM UTC-6, Robert Flatt wrote:
There is something unusual about this App, I don't know if it is relevant. But  it might generate unexpected behavior:
The widget tree is built before the build call:

I changed it by moving the `initialize_kivy()` call to the build() function.  It doesn't seem to change the functionality, but no harm in following convention
.
It's not surprising that there's something weird.  I didn't really learn to use Kivy before diving in, and this is my 3rd or 4th Kivy app, each of them stealing from whatever stumbling-through I did in the previous app.

Robert Flatt

unread,
Dec 18, 2019, 6:44:07 PM12/18/19
to Kivy users support
Well it was worth a try, I hope the super() was removed from build() too.
Reply all
Reply to author
Forward
0 new messages