Hello! I made an app with several classes of screens with repeated codes:
class screen1(Screen):
def on_pre_enter(self):
Window.bind(on_keyboard=self.voltar)
def voltar(self, window, key, *args):
if key == 27:
MDApp.get_running_app().root.transition.direction = 'right'
MDApp.get_running_app().root.current = 'menu'
return True
def on_pre_leave(self):
Window.unbind(on_keyboard=self.voltar)
class screen2(Screen):
def on_pre_enter(self):
Window.bind(on_keyboard=self.voltar)
def voltar(self, window, key, *args):
if key == 27:
MDApp.get_running_app().root.transition.direction = 'right'
MDApp.get_running_app().root.current = 'menu'
return True
def on_pre_leave(self):
Window.unbind(on_keyboard=self.voltar)
class screen3(Screen):
def on_pre_enter(self):
Window.bind(on_keyboard=self.voltar)
def voltar(self, window, key, *args):
if key == 27:
MDApp.get_running_app().root.transition.direction = 'right'
MDApp.get_running_app().root.current = 'menu'
return True
def on_pre_leave(self):
Window.unbind(on_keyboard=self.voltar)
# 18 screens...
And I want to copy the functions of one screen and paste in all others to decrease the code:
class ScreenClass():
def on_pre_enter(self):
Window.bind(on_keyboard=self.voltar)
def voltar(self, window, key, *args):
if key == 27:
MDApp.get_running_app().root.transition.direction = 'right'
MDApp.get_running_app().root.current = 'menu'
return True
def on_pre_leave(self):
Window.unbind(on_keyboard=self.voltar)
class screen1(Screen):
ScreenClass.on_pre_enter()
ScreenClass.voltar()
ScreenClass.on_pre_leave()
class screen2(Screen):
ScreenClass.on_pre_enter()
ScreenClass.voltar()
ScreenClass.on_pre_leave()
class screen3(Screen):
ScreenClass.on_pre_enter()
ScreenClass.voltar()
ScreenClass.on_pre_leave()
But I get an error, I also tried using @classmethod, but it returns an error. I tried many ways, when I don't get an error, the functions don't work :(