Kiwi Switch

I am using the Kivy python library.

I have two widgets.

When the program starts, I launch the first widget.

When this widget button is pressed, I want it to disappear and be replaced by a second widget.

Here is the .kv for two widgets

#uitest.kv <TestForm>: canvas: Rectangle: pos: self.center_x, 0 size: 10, self.height BoxLayout: size: root.size padding: 40 Button: text: 'Hello' on_release: root.testCallback() <TestForm2>: canvas: Rectangle: pos: self.center_x, 0 size: self.height, 10 

My main python file launches the application and returns the first widget

 #main.py from testform import TestForm from kivy.app import App class UITestApp(App): def build(self): return TestForm() # Main launching point if __name__ in ('__main__', '__android__'): UITestApp().run() 

My first widget has a callback. Here is the code in question

 from testform2 import TestForm2 from kivy.uix.widget import Widget class TestForm(Widget): def testCallback(self): TestForm2() # Code in question goes here. @TODO replace this widget with TestForm2 widget. 

The idea here is to have a user interface manager. This manager does not start the interface like a tree, but like a list and a stack. The list contains instances of all my user interfaces. The stack keeps going around the indicated forms, whenever we move to the form, we push it toward the stack and β€œvisualize” or something else.

EDIT: I chose my answer, but in my searches I also found a Screen object: http://kivy.org/docs/api-kivy.uix.screenmanager.html Personally, the clear () and add () commands are more powerful, but the screen takes a lot from your hands if you're interested. Transition effects too.

+7
source share
2 answers

My suggestion is to have an interface manager widget, then you can have different widgets for your user interface forms.

 import kivy from kivy.uix.label import Label from kivy.uix.button import Button from kivy.uix.boxlayout import BoxLayout from kivy.app import App class InterfaceManager(BoxLayout): def __init__(self, **kwargs): super(InterfaceManager, self).__init__(**kwargs) self.first = Button(text="First") self.first.bind(on_press=self.show_second) self.second = Button(text="Second") self.second.bind(on_press=self.show_final) self.final = Label(text="Hello World") self.add_widget(self.first) def show_second(self, button): self.clear_widgets() self.add_widget(self.second) def show_final(self, button): self.clear_widgets() self.add_widget(self.final) class MyApp(App): def build(self): return InterfaceManager(orientation='vertical') if __name__ == '__main__': MyApp().run() 

You would not structure it like this, of course. You can store all your forms in a dictionary on a Container object and have a universal callback that provides another form with a key.

 class InterfaceManager(BoxLayout): def __init__(self, **kwargs): super(InterfaceManager, self).__init__(**kwargs) self.forms = {} def add_form(self, key, form): self.forms[key] = form def uniCallback(self, button): self.clear_widgets() # You could introduce a more elegant key # handling system here. self.add_widget(self.forms[button.text]) 
+9
source

A simple way to switch widgets is that one of them is the full height of its container, and the second has a height of zero. When you need to switch, just change the heights around.

+4
source

All Articles