KivyMapView将按钮控件放置在mapview widg的顶部

2024-04-25 21:31:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在Kivy地图视图中添加一些小部件

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

但不管我给它什么布局选项,它总是卡在左下角。即使我添加了更多的小部件,它们也只是堆叠在左下角的顶部。我的目标是在右上角创建一个按钮,使地图位于用户的中心。在


Tags: textself视图bind部件地图buttonlon
1条回答
网友
1楼 · 发布于 2024-04-25 21:31:04
  1. 使用Kivy Anchor Layout。在
  2. 如果要添加更多按钮,我的建议是使用Kivy Drop-Down List。在

示例

在主.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()

输出

Img01 - Unlock Button Top Right Corner

相关问题 更多 >