0

我正在使用 kivy.garden.mapviev 创建一个 android 应用程序,我希望用户能够使用按钮移动标记(英雄图标)。当我将一个 int 值添加到从字符串转储到浮点数的值中时,我将结果转换回字符串。当我尝试这样做时,我得到一个错误。我在一个文件中执行整个应用程序代码,屏幕管理器是文件中的一个字符串,作为 screen_helper

当我尝试执行代码时,得到主题中给出的错误。我是新手:kivy / kivyMD,下面是应用程序代码和屏幕截图:。

screen_helper = """
ScreenManager:
    UsersPlayGameOnMap:
<UsersPlayGameOnMap>:
    name: 'screenmapmove'
    MapView:
        id: mapview
        lat: 40.41362602642995
        lon: -3.6819590868909984 
        zoom:19        
        max_zoom : 19
        min_zoom :19
        MapMarkerPopup:
            id: player_position
            source: "img/myicons/heromenuicon.png"
            lat: 40.41362602642995
            lon: -3.6819590868909984    
    MDIconButton :
        icon : "apps-box" 
        pos_hint: {'center_x':0.1,'center_y':0.1}
        user_font_size : 40 
        on_press: root.manager.current = 'UserPlatformFunctions'
    MDIconButton : 
        id : up  
        icon : "arrow-up-bold-box-outline"       
        pos_hint : {'center_x':0.5,'center_y':0.18}
        user_font_size : 40 
        on_press: root.buttonUP()
    MDIconButton : 
        id : down 
        icon : "arrow-down-bold-box-outline"       
        pos_hint : {'center_x':0.5,'center_y':0.1}
        user_font_size : 40 
        on_press: root.button_DOWN()

    MDIconButton : 
        id : right 
        icon : "arrow-right-bold-box-outline"       
        pos_hint : {'center_x':0.65,'center_y':.1}
        user_font_size : 40 
        on_press: root.button_RIGHT()
    MDIconButton :
        id : left  
        icon : "arrow-left-bold-box-outline"       
        pos_hint : {'center_x':0.35,'center_y':0.1}
        user_font_size : 40 
        on_press: root.button_LEFT()

"""

LATI=40.41362602642995
LONDI=-3.6819590868909984
SETTING = True

class UsersPlayGameOnMap(Screen):


    def buttonUP(self):
        self.pressUP = True

        self.pressDOWN = False
        self.pressLEFT = False
        self.pressRIGHT = False

        self.LoadPlayerObject()


    def button_RIGHT(self):
        self.pressRIGHT = True

        self.pressDOWN = False
        self.pressUP = False
        self.pressLEFT = False

        self.LoadPlayerObject()


    def button_LEFT(self):
        self.pressLEFT = True

        self.pressRIGHT = False
        self.pressDOWN = False
        self.pressUP = False

        self.LoadPlayerObject()


    def button_DOWN(self):
        self.pressDOWN = True

        self.pressUP = False
        self.pressLEFT = False
        self.pressRIGHT = False

        self.LoadPlayerObject()


    def LoadPlayerObject(self):

        if SETTING == False:
            self.player_pos_pion = self.localpion
            self.player_pos_poz = self.localpoz


        if SETTING == True:
            self.ids.mapview.lat = LATI
            self.ids.mapview.lon = LONDI
            self.player_pos_pion = LATI
            self.player_pos_poz = LONDI
            self.SETTING = False

        longitude = float(self.player_pos_poz)
        latitude = float(self.player_pos_pion)

        if self.pressUP == True:
             latitude +=0.001

        if self.pressDOWN == True:
            latitude -=0.001

        if self.pressLEFT == True:
            longitude -=0.0001

        if self.pressRIGHT == True:
            longitude +=0.0001

        self.localpoz = longitude
        self.localpion = latitude
        self.PLAYER_POSITION()


    def PLAYER_POSITION(self):

        my_lat = NumericProperty(self.localpion)
        my_lon = NumericProperty(self.localpoz)

        playerpos = App.get_running_app().root.ids.player_position
        playerpos.lat = NumericProperty(my_lat)
        playerpos.lon = NumericProperty(my_lon)
        mapposition = App.get_running_app().root.ids.mapview
        mapposition.center_on(my_lat,my_lon)


sm = ScreenManager()
sm.add_widget(UsersPlayGameOnMap(name='screenmapmove'))

class gameapp(MDApp):

    def build(self):
        self.screen = Builder.load_string(screen_helper)
        return self.screen


gameapp().run()

屏幕

4

1 回答 1

0

您的错误是您正在尝试访问您的小部件中的player_position(以及后来的mapview)ID ,即. 但是这些 id 是在 的规则中定义的,所以这就是这些 id 所在的位置。要访问这些 ID,您需要通过. 像这样的东西:rootAppScreenManagerUsersPlayGameOnMapUsersPlayGameOnMap

    game_screen = MDApp.get_running_app().root.get_screen('screenmapmove')
    playerpos = game_screen.ids.player_position
    .
    .
    .
    mapposition = game_screen.ids.mapview

在同一代码中,您在使用NumericProperty. Properties应该在类内部定义,但在任何方法之外。请参阅文档

于 2021-12-17T20:30:58.563 回答