Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to add some widgets to a Kivy mapview like this

mapview = self.mapWidget(self.lat, self.lon)        
touchBarbtn1 = Button(text='Unlock')
touchBarbtn1.bind(on_press=lambda x: self.centerOnUser())
mapview.add_widget(touchBarbtn1)

But no matter what layout options I give it, it's always stuck in the bottom left corner. Even if I add more widgets they all just stack on top of each other in the bottom left corner. My Goal is to make a button on the top right corner to center the map on the user.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
347 views
Welcome To Ask or Share your Answers For Others

1 Answer

  1. Use Kivy Anchor Layout.
  2. If you are going to add more buttons, my suggestion is to use Kivy Drop-Down List.

Example

main.py

from kivy.garden.mapview import MapView
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.properties import NumericProperty


class RootWidget(AnchorLayout):
    lat = NumericProperty(50.6394)
    lon = NumericProperty(3.057)

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        self.anchor_x = 'right'
        self.anchor_y = 'top'
        mapview = MapView(zoom=11, lat=self.lat, lon=self.lon)
        self.add_widget(mapview)
        touchBarbtn1 = Button(text='Unlock', size_hint=(0.1, 0.1))
        touchBarbtn1.bind(on_press=lambda x: self.centerOnUser())
        self.add_widget(touchBarbtn1)

    def centerOnUser(self):
        pass


class MapViewApp(App):
    def build(self):
        return RootWidget()


MapViewApp().run()

Output

Img01 - Unlock Button Top Right Corner


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...