Label anchors

153 views
Skip to first unread message

Minsc Boo

unread,
Jun 8, 2014, 6:40:40 PM6/8/14
to kivy-...@googlegroups.com
Hi everybody!
I've been using kivy in the last two week, so i'm really noob :P

I have a problem with Label anchors propriety: basically, it returns (0, 0) for every anchor.
I've looked in 1.8.0 and in git-master, a lot seem to have changed
Before saying if i could help, i wanted to know if it's a known problem or if you're working on this right now
(i'm using label inside a scrollview, if that matter).

i've also tested refs and they work great.

Anyway i'm enthusiast for kivy!!
I hope i could learn some, and maybe tomorrow be helping you :)

ZenCODE

unread,
Jun 9, 2014, 1:58:36 AM6/9/14
to kivy-...@googlegroups.com
Hi

Please could you paste a minimal example of the code? That way we can confirm that's it's not maybe something you are doing or not doing? ;-)

Cheers

Minsc Boo

unread,
Jun 9, 2014, 5:10:50 AM6/9/14
to kivy-...@googlegroups.com

Here a very minimal example, i found out it in a more complex situation where the label is inside a scrollview, and has
a trasparent background etc etc

I'm delaying the "control print" to be sure to ask "after rendering" (as i've read on the docs)

#!/usr/bin/python
from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock


test
= 'ciao mondo\n[anchor=xxx]tutto bene?\n[anchor=yyy]sperem...[anchor=middleline]foo\n'


class myApp(App):
 
def on_start(self):
   
Clock.schedule_once(self.printa,4)
 
def printa(self,*dt):
   
print self.l.anchors
 
def build(self):
   
self.l=Label(text=test,markup=True)
   
return self.l


myApp
().run()



ZenCODE

unread,
Jun 10, 2014, 3:44:18 AM6/10/14
to kivy-...@googlegroups.com
Firstly, thanks for taking the time to (a) read the docs and (b) post a minimal example. I can confirm that running 1.8.0 on windows, I also get incorrect info on the anchors.

    {u'xxx': (0, 18.0), u'yyy': (0, 18.0), u'middleline': (64, 18.0)}

I will try this again later on my home machine (linux, running the latest dev version) and post the results. And yay for the enthusiasm. I'm a kivy nut myself, loving it.... ;-)

Cheers


Minsc Boo

unread,
Jun 10, 2014, 6:13:21 AM6/10/14
to kivy-...@googlegroups.com
 yes, i looked a little into core.text.markup, (line ~205-214 and then just a watch into the real parsing, but it's a big beast for me :P)
 basically in 1.8.0 that "lines" used for parsing, it's a list of list, but now in git-master it's a list of dict

The thing is, i haven't gone trough the real parsing, but since refs give right value, shouldn't be too hard to make anchors work
(i hope :) )
if i have time i give it a look, but no promise :P

ZenCODE

unread,
Jun 11, 2014, 2:24:14 AM6/11/14
to kivy-...@googlegroups.com
Yes, I can confirm that it's still happening on master.

     {u'xxx': (0, 0), u'yyy': (0, 0), u'middleline': (0, 0)}

I'll create a ticket for that and also try looking into it...;-)

Cheers

ZenCODE

unread,
Jun 14, 2014, 10:52:21 AM6/14/14
to kivy-...@googlegroups.com
FYI: Have submitted a PR to fix the issue. If you're bored, you can confirm that it works...;-)

https://github.com/kivy/kivy/pull/2259/files

ZenCODE

unread,
Jun 15, 2014, 3:36:18 AM6/15/14
to kivy-...@googlegroups.com
ps. That PR just mimicked how the "ref" property works, and we now have co-ordinates relative to the text block. But how do we get the position of the text block? I'm trying to write a demo that outlines the refs and anchors, but cannot seem to get hold of the text position. Do you/does anyone know how to use the refs/anchors to determine the real coordinates of those areas on the Label's canvas?

Cheers

Minsc Boo

unread,
Jun 15, 2014, 2:53:40 PM6/15/14
to kivy-...@googlegroups.com
In that experiment that i'm making, i have a long label inside scrollview where i print text within canvas_drawed icons 
(i will tomorrow try to use an "add_wdiget(Image ... )" inside the label - seems prettier if the label get resized etc)
I use a "[ref=%s]       [/ref]" % number, to find where to place the icon (yes a bit row, but they're all small icons)

Basically, the coords returned in Label.refs, are top-left coordinate... i'm using a fixed size label, but i think it would do the same
with a size_hint placed Label.

i wrote a function like these for know if the mouse/touch is over a ref: 

(copied from kivy.uix.label on_touch_down line ~208 and rearranged)

  def is_ref(self,touch):

   
# self is the scrollview, self.lbl the label inside the scrollview
    tx
, ty = touch.pos

   
# as i understand, here they're subtracting the position of the upper-left corner, to have a
   
# relative coordinate to the label
    tx
-= self.center_x - self.lbl.texture_size[0] / 2.
    ty
-= self.center_y - self.lbl.texture_size[1] / 2.

   
# and here "invert" the y
    ty
= self.lbl.texture_size[1] - ty

   
# below is almost the same as found in on_touch_down
   
for uid, zones in self.lbl.refs.items():
     
for zone in zones:
        x
, y, w, h = zone
       
if x <= tx <= w and y <= ty <= h:
         
return uid
   
return False


i hope it helps :)

i've looked a bit inside core.text.markup, but i should look in deeper ..
then, i discovered treeview and popup and .... too many good things :P

ZenCODE

unread,
Jun 15, 2014, 3:08:18 PM6/15/14
to kivy-...@googlegroups.com
Brilliant! Was obsessed about the x and y, but using center_x/center_y works perfectly. Thanks * 10 :-)

Minsc Boo

unread,
Jun 15, 2014, 3:45:48 PM6/15/14
to kivy-...@googlegroups.com
well, i thought to not be very helpful, so i made a fast example

#!/usr/bin/python


from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.graphics import *


test
= 'ciao mondo\n[ref=xxx]tutto bene?[/ref]\n[ref=yyy]sperem...[/ref][ref=middleline]foo[/ref]\n'




class myApp(App):
 
def on_start(self):
   
Clock.schedule_once(self.outline,4)
 
def outline(self,*dt):


   
for uid,chars in self.l.refs.items():
     
# from first word, i take x1, y1
      sx
,sy = chars[0][:2]
     
# from last character, i take x2, y2
      ex
, ey = chars[-1][2:]
     
# find top-left corner of real render
      tx
= self.l.center_x - self.l.texture_size[0]/2 #left
      ty
= self.l.center_y + self.l.texture_size[1]/2 #top
     
# find actual bounding box
      sx
, ex = tx+sx, tx+ex
      sy
, ey = ty-sy, ty-ey
     
self.l.canvas.add(Line(rectangle=(sx,sy,ex-sx,ey-sy)))

 
 
def build(self):
   
self.l=Label(text=test,markup=True)
   
return self.l




myApp
().run()

as  a note, using the center_x,center_y is a way to get the real label render place, so

 i'm using a fixed size label, but i think it would do the same with a size_hint placed Label.

obviously is not true. (the coordinate are relative to the actual render, not the widget) 



ZenCODE

unread,
Jun 15, 2014, 5:18:27 PM6/15/14
to kivy-...@googlegroups.com
Thanks, the center insight was the key for me here.  Have added an example to the documentation so it's easier for others...:-)

https://github.com/kivy/kivy/commit/fd2ab6e31365b0d20acd247c1662735106024261

Chjeers

ZenCODE

unread,
Jun 15, 2014, 5:24:35 PM6/15/14
to kivy-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages