Very weird problem. Need experienced eyes!

21 views
Skip to first unread message

Steven Summers

unread,
May 23, 2020, 9:22:10 PM5/23/20
to Kivy users support
I'm trying to make a widget with four labels, one per corner. I'm using a RelativeLayout and setting their pos values assuming the lower left corner of the rectangle is 0,0.  The code looks like this: 

from kivy.app import App
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout

class Quadlabel(RelativeLayout):

    def __init__(self, **kwargs):
        super(Quadlabel, self).__init__(**kwargs)
        print("self.size, size_hint, pos, pos_hint are:", self.size, self.size_hint, self.pos, self.pos_hint)
        with self.canvas.before:
            Color(0.2, 0.2, 0.2, 1)
            self.rect = Rectangle(size=self.size)
        self.TL = Label(text="TL", bold=True, color=(1,0,0,1), pos=(0.25*self.width, 0.7*self.height))
        self.add_widget(self.TL)
        self.TR = Label(text="TR", bold=True, color=(1,1,0,1), pos=(0.75*self.width, 0.7*self.height))
        self.add_widget(self.TR)
        self.BR = Label(text="BR", bold=True, color=(0,1,0,1), pos=(0.75*self.width, 0.3*self.height))
        self.add_widget(self.BR)
        self.BL = Label(text="BL", bold=True, color=(0,0,1,1), pos=(0.25*self.width,  0.3*self.height))
        self.add_widget(self.BL)

class MainApp(App):

    def build(self):
        self.root = FloatLayout()
        self.root.add_widget(Quadlabel(size=(96,96), pos=(100, 0)))
        self.root.add_widget(Quadlabel(size=(96,96), pos=(0, 100)))

if __name__ == '__main__':
    Window.size = (500, 400)
    MainApp().run()

When I run this, the Print statements for the two Quadlabels look like:

self.size, size_hint, pos, pos_hint are: [96, 96] [1, 1] [100, 0] {}
self.size, size_hint, pos, pos_hint are: [96, 96] [1, 1] [0, 100] {}

When I run it, I get this: 

Kivybug.PNG


I've been pounding my head against the wall for several hours trying to make sense of this. I can't find anything that would explain the offsets of the labels compared to the rectangles. I know there has to be SOMETHING that's offsetting the positions of the labels, and I read this part pretty carefully, but it doesn't apply - if I change the "BR" pos=() expression to "pos=(0,0)", I get this: 

Kivybug2.PNG


So clearly, position 0,0 is relative - it's just relative to the wrong place, and I can't figure out where it's getting the offset from! 

I'm not sure if I hope one of you bright young people can spend 10 seconds and say "Oh! Right there!" but is it possible it's a bug?  

In case it's helpful, here is what Kivy reports in my console on startup:

[INFO   ] [Logger      ] Record log in C:\Users\SES\.kivy\logs\kivy_20-05-23_49.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.gstreamer" 0.2.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.2.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.2.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.2.0
[INFO   ] [Kivy        ] v1.11.1
[INFO   ] [Kivy        ] Installed at "C:\Prog\Python37\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Prog\Python37\python.exe"
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 NVIDIA 445.87'>
[INFO   ] [GL          ] OpenGL vendor <b'NVIDIA Corporation'>
[INFO   ] [GL          ] OpenGL renderer <b'GeForce RTX 2070 SUPER/PCIe/SSE2'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 NVIDIA'>
[INFO   ] [GL          ] Texture max size <32768>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available

Please help me!!!

Elliot Garbus

unread,
May 23, 2020, 10:30:51 PM5/23/20
to kivy-...@googlegroups.com

You are positioning the labels based on the height and width of the labels.  I’d recommend using pos_hint, to position the labels.

 

I've been pounding my head against the wall for several hours trying to make sense of this. I can't find anything that would explain the offsets of the labels compared to the rectangles. I know there has to be SOMETHING that's offsetting the positions of the labels, and I read this part pretty carefully, but it doesn't apply - if I change the "BR" pos=() expression to "pos=(0,0)", I get this: 

 

--
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/4b507ca0-e53e-44c7-9a13-a4fc316f5da0%40googlegroups.com.

 

Steven Summers

unread,
May 23, 2020, 11:10:08 PM5/23/20
to Kivy users support
I don't think so - I'm using fractions of the height and width of self- the Quadlabel, which is a RelativeLayout.  Self.width/self.height are 96,96, so the offsets from the lower left corner of that should be less than an inch, and inside the gray boxes. As I pointed out, even using "pos=(0,0)" STILL puts the label far outside the rectangle. 

Also, if I change the line starting with "self.TL" to 
  self.TL = Label(text="TL", bold=True, color=(1,0,0,1), pos_hint={'center_x':0.25, 'center_y':0.7})
I get this: 

Kivybug3.PNG


That's not anywhere inside the RelativeLayout area either. 

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Steven Summers

unread,
May 24, 2020, 12:04:43 AM5/24/20
to Kivy users support
Found it!  

The culprit was the size_hint that I wasn't passing into the Quadlabel - it defaults to [1,1].  So by passing the actual size, but not a size hint, it recalculated the sizes and positions of things when it was done "filling the layout", and decided my little 96x96 pixel "box" was the size of the entire window, and centered the 0,0 point accordingly. [I think, anyway.]

Bottom line: I added "self.size_hint=(None, None)", and switched back to setting position with code like "pos_hint={'center_x':0.2, 'center_y',0.8"), it now works, and I now understand a bit better (I hope!) what's going on under the hood.  I also see why using KV with its automatic bindings of property changes is going to be helpful.    

Elliot Garbus

unread,
May 24, 2020, 12:14:53 AM5/24/20
to kivy-...@googlegroups.com

If you have not used the inspector, I highly recommend it. 

On the command line type:  python myapp.py -m inspector

 

Then press cntrl-e and click on the widgets to see what they are.  Experiment with the tool – it is outstanding. All the buttons show more info.  There are somethings you can edit right in the tool.

 

We did it at the same time…

 

from kivy.app import App
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout

class Quadlabel(RelativeLayout):

   
def __init__(self, **kwargs):
       
super(Quadlabel, self).__init__(**kwargs)
       
print("self.size, size_hint, pos, pos_hint are:", self.size, self.size_hint, self.pos, self.pos_hint)
       
with self.canvas.before:
            Color(
0.2, 0.2, 0.2, 1)
           
self.rect = Rectangle(size=self
.size)
       
self.TL = Label(text="TL", bold=True, color=(1,0,0,1), pos=(0.25*self.width, 0.7*self.height),
                        
size_hint=(None,None), size=(10,10))
       
self.add_widget(self.TL)
       
self.TR = Label(text="TR", bold=True, color=(1,1,0,1), pos=(0.75*self.width, 0.7*self.height),
                       
size_hint=(None,None), size=(10,10))
       
self.add_widget(self.TR)
       
self.BR = Label(text="BR", bold=True, color=(0,1,0,1), pos=(0.75*self.width, 0.3*self.height),
                       
size_hint=(None,None), size=(10,10))
       
self.add_widget(self.BR)
       
self.BL = Label(text="BL", bold=True, color=(0,0,1,1), pos=(0.25*self.width,  0.3*self.height),
                       
size_hint=(None,None), size=(10,10))
       
self.add_widget(self.BL)

class Main99App(App):

   
def build(self):
       
self.root = FloatLayout()
       
self.root.add_widget(Quadlabel(size=(96,96), pos=(100, 0)))
       
self.root.add_widget(Quadlabel(size=(96,96), pos=(0, 100)))
       
return self.root

if __name__ == '__main__':
    Window.size = (
500, 400
)
    Main99App().run()

 

 

From: Steven Summers
Sent: Saturday, May 23, 2020 9:04 PM
To: Kivy users support

 

I've been pounding my head against the wall for several hours trying to make sense of this. I can't find anything that would explain the offsets of the labels compared to the rectangles. I know there has to be SOMETHING that's offsetting the positions of the labels, and I read this part pretty carefully, but it doesn't apply - if I change the "BR" pos=() expression to "pos=(0,0)", I get this: 

 

--

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/2b3a9a13-9c46-43ea-9e42-877355de22ea%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages