如何添加kivy.garden.mapview公司在kivy应用程序中使用屏幕管理

2024-09-28 22:23:13 发布

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

我基本上是想补充kivy.garden.mapview公司作为kivy应用程序的小部件。在

我就是这么想的。在

我还添加了Painter类,因为如果我使用Painter作为小部件,它可以完美地工作,但是Map类则不行。 另外,Map if作为一个应用程序测试时返回的mapview非常好用。在

from kivy.app import App
# kivy.require("1.10.1")
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.garden.mapview import MapView,MapMarker

from kivy.uix.widget import Widget
from kivy.graphics import Line

xList = [3.2,4]
yList = [2.3,3]

class Map(Widget):
    def on_touch_down(self, touch):
        global xList
        global yList
        mapview = MapView(zoom=1, lat=67, lon=42)
        m1 = MapMarker(lat=67, lon=42)
        mapview.add_marker(m1)
        print(xList)
        print(yList)
        for i in range(len(xList)):
            m=MapMarker(lat=xList[i],lon=yList[i])
            mapview.add_marker(m)
        # return mapview


class Painter(Widget):

    def on_touch_down(self, touch):
        with self.canvas:
            touch.ud["line"] = Line(points=(touch.x, touch.y))

    def on_touch_move(self, touch):
        touch.ud["line"].points += [touch.x, touch.y]


class MainScreen(Screen):
    pass


class AnotherScreen(Screen):
    pass


class ScreenManagement(ScreenManager):
    pass


presentation = Builder.load_file("SimpleKivy.kv")


class MainApp(App):

    def build(self):
        return presentation


if __name__ == "__main__":
    MainApp().run()

kivy档案是

^{pr2}$

那我该怎么做呢?在

我可以不让地图成为一个小部件吗?在

我需要一个图表,所以我需要至少有2个屏幕。在


Tags: fromimportselfmap部件defscreenclass
1条回答
网友
1楼 · 发布于 2024-09-28 22:23:13

实际上,您没有将MapView添加到App中。您可以在您的Map小部件中引用它,但永远不会将其添加到App中。一种方法是将Map更改为扩展MapView

class Map(MapView):
    def on_touch_down(self, touch):
        global xList
        global yList
        m1 = MapMarker(lat=67, lon=42)
        self.add_marker(m1)
        print(xList)
        print(yList)
        for i in range(len(xList)):
            m=MapMarker(lat=xList[i],lon=yList[i])
            self.add_marker(m)

kv文件中,添加Map的初始设置:

^{pr2}$

相关问题 更多 >