Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

python - Kivy mapview - Place button widgets ontop of mapview widget

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

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

1 Reply

0 votes
by (71.8m points)
  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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...