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
560 views
in Technique[技术] by (71.8m points)

python - How can I make a map screen in kivy, I only get black screen

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy_garden.mapview import MapView
kv = '''

<Login>
    ben: benName.text
    pw: passwort.text
    knopf: btn
    knopff: btnn
 
    GridLayout:
        cols: 1
        size: root.width,root.height
        GridLayout:
            cols: 2
            Label:
                text: "Username"
                font_size: 25
            TextInput:
                id: benName
                multiline: False
                font_size: 30
            Label:
                text: "Password"
                font_size: 25
                bold: True
            TextInput:
                password: True
                id: passwort
                multiline: False
                font_size: 40
        Button:
            size_hint: (1.,1.10)
            text:" Start "
            id: btn
            font_size: 40
            on_release:
                root.manager.current = "Map" if passwort.text == "1" and benName.text == "1" else "login"
                root.manager.transition.direction = "down"
        Button:
            size_hint: (1.,1.10)
            text: " Exit "
            id: btnn
            font_size: 40
            on_release: app.stop()


<Map>
    BoxLayout:
        Button:
            text: " [back] "
            bold: True
            font_size: 17
            size_hint: (None,None)
            width: 100
            height: 40
            on_release: app.stop()

'''

MyApp class:

class Login(Screen):
    ben = StringProperty()
    pw = StringProperty()
    knopf = ObjectProperty()


class MyApp(App):
    Builder.load_string(kv)
 
    def build(self):
        ms = ScreenManager()
        ms.add_widget(Login(name='login'))
        ms.add_widget(Map(name='Map'))
        self.title = "MyApp"
        return ms






class Map(Screen):
    def build(self):
        mapview = MapView(zoom=11, lat=50.6394, lon=3.057)
        return mapview


if __name__ == '__main__':
    MyApp().run()

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

1 Reply

0 votes
by (71.8m points)

Your build() method in the Map class is never called (it is not called automatically). I suggest you change the build() method to an on_enter() method:

class Map(Screen):
    def on_enter(self, *args):
        mapview = MapView(zoom=11, lat=50.6394, lon=3.057)
        self.ids.map.add_widget(mapview)

And add the map id to the Map rule in the kv:

<Map>
    BoxLayout:
        id: map
        Button:
            text: " [back] "
            bold: True
            font_size: 17
            size_hint: (None,None)
            width: 100
            height: 40
            on_release: app.stop()

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

...