Python | Tabbed panel in kivy

Python | Tabbed panel in kivy

To create a tabbed panel in Kivy, you can use the TabbedPanel widget. Kivy's TabbedPanel allows you to organize content in tabs, with each tab containing different widgets.

Below is a basic example of how to create a TabbedPanel with several tabs, each containing some content:

from kivy.app import App from kivy.uix.tabbedpanel import TabbedPanel from kivy.uix.label import Label from kivy.lang import Builder # Define the Kivy UI layout for the app using the KV language Builder.load_string(""" <TabbedPanelExample>: do_default_tab: False # Disables the default tab tab_pos: 'top_mid' # Position of the tabs ('top_mid' places tabs in the center at the top) # Define the tabs TabbedPanelItem: text: 'Tab 1' Label: text: 'Content of tab 1' TabbedPanelItem: text: 'Tab 2' Label: text: 'Content of tab 2' TabbedPanelItem: text: 'Tab 3' Label: text: 'Content of tab 3' """) # Extend the TabbedPanel class to create your custom UI class TabbedPanelExample(TabbedPanel): pass # The App class which builds the app class TabbedPanelApp(App): def build(self): return TabbedPanelExample() # Run the app if __name__ == '__main__': TabbedPanelApp().run() 

In this example:

  • The Builder.load_string function is used to define the UI layout using Kivy's KV language, which allows for a clean separation of layout and logic.
  • We create a subclass of TabbedPanel called TabbedPanelExample where we define the layout of our tabbed panel.
  • The do_default_tab: False line disables the default tab that Kivy creates.
  • We then define three TabbedPanelItem objects, each with a label as its child to represent the content of each tab.
  • The TabbedPanelApp class is our main application class which creates an instance of our TabbedPanelExample.
  • Finally, we run the app.

To see this in action, make sure you have Kivy installed in your Python environment, then run this script. You'll see a window with a tabbed panel at the top, and you can click on the tabs to switch between the different content.


More Tags

coordinator-layout bootbox cell-formatting pointer-arithmetic haversine visible avplayer linearlayoutmanager google-places-api prettier

More Programming Guides

Other Guides

More Programming Examples