如何使用屏幕管理器将 kivy.garden.mapview 添加到 kivy 应用程序中的屏幕
How to add kivy.garden.mapview to a screen in a kivy app using screen manager
我基本上是在尝试将 kivy.garden.mapview 作为小部件添加到 kivy 应用程序中。
我就是这样尝试的。
我还添加了 Painter class,因为如果我将 Painter 用作小部件,它会完美地工作,但 Map class 不会。
另外 Map 如果作为应用程序进行测试并取消注释 return 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文件是
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
AnotherScreen:
<MainScreen>:
name: "main"
Button:
on_release: app.root.current = "Map"
text: "Map"
font_size: 8
size_hint:0.05,0.05
pos_hint: {"right":1,"top":1}
color: 0,1,0,1
<AnotherScreen>:
name: "Map"
FloatLayout:
Map
Button:
color: 0,1,0,1
font_size: 8
size_hint: 0.05,0.05
text: "Graph"
on_release: app.root.current = "main"
pos_hint: {"right":1, "top":1}
那我应该怎么做呢?
我可以不把地图做成小部件吗?
我需要一个图表,所以我至少需要有 2 个屏幕。
您实际上还没有将 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
的初始设置:
Map:
zoom: 1
lat: 67
lon: 42
使用 POPUP
class mappopup(RelativeLayout):
def get(self):
x=self.ids.latitude.text
print(x)
print(self.ids.longitude.text)
def openmap(self):
popup = Popup(content=mappopup())
popup.open()
<mappopup>:
MapView:
id: mapview
lat: 20.9517
lon: 85.0985
zoom: 8
MapMarker:
lat: mapvie w.lat
lon: mapview.lon
Toolbar:
Label:
id: latitude
text: "Longitude: {}".format(mapview.lon)
Label:
id: longitude
text: "Latitude: {}".format(mapview.lat)
Button:
text:"OK"
on_release:root.get()
我基本上是在尝试将 kivy.garden.mapview 作为小部件添加到 kivy 应用程序中。
我就是这样尝试的。
我还添加了 Painter class,因为如果我将 Painter 用作小部件,它会完美地工作,但 Map class 不会。 另外 Map 如果作为应用程序进行测试并取消注释 return 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文件是
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
AnotherScreen:
<MainScreen>:
name: "main"
Button:
on_release: app.root.current = "Map"
text: "Map"
font_size: 8
size_hint:0.05,0.05
pos_hint: {"right":1,"top":1}
color: 0,1,0,1
<AnotherScreen>:
name: "Map"
FloatLayout:
Map
Button:
color: 0,1,0,1
font_size: 8
size_hint: 0.05,0.05
text: "Graph"
on_release: app.root.current = "main"
pos_hint: {"right":1, "top":1}
那我应该怎么做呢?
我可以不把地图做成小部件吗?
我需要一个图表,所以我至少需要有 2 个屏幕。
您实际上还没有将 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
的初始设置:
Map:
zoom: 1
lat: 67
lon: 42
使用 POPUP
class mappopup(RelativeLayout):
def get(self):
x=self.ids.latitude.text
print(x)
print(self.ids.longitude.text)
def openmap(self):
popup = Popup(content=mappopup())
popup.open()
<mappopup>:
MapView:
id: mapview
lat: 20.9517
lon: 85.0985
zoom: 8
MapMarker:
lat: mapvie w.lat
lon: mapview.lon
Toolbar:
Label:
id: latitude
text: "Longitude: {}".format(mapview.lon)
Label:
id: longitude
text: "Latitude: {}".format(mapview.lat)
Button:
text:"OK"
on_release:root.get()