有什么办法可以在Kivy中使用id访问其他类对象?

时间:2019-01-22 11:27:03

标签: python kivy

我正在开发一种方法来更改Kivy应用程序中的语言。

我正在尝试使用 id 从其他类访问对象。

我的.py代码的一部分:

    class SettingsScreen(Screen):
        def change_langg(self):

           #MainMenu screen
           self.main = MenuScreen()
           self.exit_but = self.main.ids['exit']
           #Settings screen
           self.back1_but = self.ids['back1']
           self.lang_but = self.ids['language']
           self.fullscr_but = self.ids['fullscr']

           if self.lang_but.text == 'Бг':

              #Main menu screen
               self.exit_but.text = 'Изход'
               #Settings screen
               self.back1_but.text = 'Назад'
               self.fullscr_but.text = 'Цял екран'
               self.lang_but.text = 'En'

           elif self.lang_but.text == 'En':
              #Main menu screen
              self.exit_but.text = 'Exit'
              #Settings screen
              self.back1_but.text = 'Back'
              self.fullscr_but.text = 'Fullscreen'
              self.lang_but.text = 'Бг'

我希望文本在两个屏幕中都会更改,但仅在“设置”屏幕中会更改。如上所述,我没有任何错误,所有ID均正确。你有什么建议吗?

1 个答案:

答案 0 :(得分:0)

即使在显示屏幕后,我也可以使用一种解决方案。代码和注释应该是自解释的,否则请与我联系以了解不清楚的部分

othrscrn = '' #global variable where the memory location of the other screen can be saved

    class OtherScreen(Screen):
        def __init__(self, *args): #init method to run immediately the code is started
            super(OtherScreen, self).__init__(*args)
            global othrscrn #declaring variable 'othrscrn' a global variable
            othrscrn = self #saving the class' memory location to global variable 'othrscrn'

    class Settings(Screen):
            def change_lang(self):
                global othrscrn #accessing the variable 'othrscrn'
                x = othrscrn #instanciating the class as a new variable x
                lan = x.ids['language'] #acessing the class' ids using its ids
                lan.text = ''#any chnage you want to implement