Skip to content

Instantly share code, notes, and snippets.

@winstonwolff
Last active June 30, 2018 08:55
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save winstonwolff/6356247 to your computer and use it in GitHub Desktop.
I'm trying to layout a widget like this:
----------------------------
| [IMAGE] |
|[.....a label here.......]|
----------------------------
Where the image is centered at the top, and the label is centered at the bottom.
Here's some code:
-----------------------------
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.factory import Factory
class KivyCenterBug(App):
def build(self):
m = Factory.Marker(pos=(400, 100))
self.root.add_widget(m)
if __name__ == '__main__':
KivyCenterBug().run()
--------------------------------
# KV file
Widget:
Marker:
pos: 400, 500
Label:
pos: 10, 10
width: root.width - 20
text_size: self.size
text: 'Top marker is positioned in KV file. Bottom marker is positioned by python. Center widget in bottom row has the image misalligned.'
<Marker@Widget>:
puzzle_name: ''
size: 300, 200
DebugArea
Image:
size: self.texture_size # !!! Image's size is set to texture_size
id: the_image
center_x: root.center_x
top: root.top
source: 'puzzle_dot.png'
DebugArea
Label:
size: root.width, 100
text_size: self.size
center_x: root.center_x
y: root.y
text: 'This marker tries to resize Image to its texture_size. It becomes misalligned when positioned by Python, but not when positioned by KV'
DebugArea
<DebugArea@Widget>:
# Draw an X to show where the parent widget is.
size: self.parent.size
pos: self.parent.pos
canvas:
Color:
rgba: 1, 0, 1, .3
Line:
points: root.x, root.y, root.x + root.width, root.y + root.height
Line:
points: root.x, root.y + root.height, root.x + root.width, root.y
Line:
rectangle: root.x + 2, root.y + 2, root.width - 4, root.height - 4
Label:
color: 1, 0, 1, 0.7
font_size: 12
center: root.center
text: '{root.parent.__class__.__name__}'.format(root=root, pos=root.pos, size=root.size, pos_hint=root.parent.pos_hint, size_hint=root.parent.size_hint)
@winstonwolff
Copy link
Author

Screenshot:
screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment