Kivy: Screenmanager() to Clear Widgets before screen transition

Asked

Viewed 274 times

0

I’m building an application with Python and Kivy.

To toggle the screens use Kivy’s Screenmanager. Ex.:

Button:
    text: 'Sair de Tela 2 e voltar para Tela 1'
    size_hint_y: None
    height: 100
    on_press: root.manager.current = 'tela1'

However, on Screen 2 I add and delete items from a list, among other things that change the screen layout. And I need that when I leave this screen, everything is clean/reset, so that when I return the screen is clean and also so that it does not overload the application with unnecessary items saved in the layout.

I know the method self clear_widgets(), But I would like to know if Screenmanager() has any property or method that does this? Because if I have to create a function that makes the screen transition, using clear_widgets() to clean the screen... I think it is impossible to continue with Screenmanager()

  • there is the method root.manager.clear_widgets()

1 answer

0

Hi, you can use to remove widgtes from a screen using the function remove_widget(). As your screen can have child widgtes (widgets that are inside it) you can remove them using the Children attribute from your screen.

In the following code I have a screen and when coming out of it all widgets of it will be redeemed:

class Tela(Screen):
     def on_pre_leave(self):
         #Função para configurar eventos antes de sair
         for widget in range(len(self.children)):
          #Estrutura de repetição para contar os seus 
          #widgets filhos e transforma-los em uma lista
               self.remove_widget(self.children[0])
          #Função que remove o widget filho na posição 0
          #a cada volta, um widget com índice maior torna-se 
          #o primeiro(0) e é removido

Browser other questions tagged

You are not signed in. Login or sign up in order to post.