Kivy – Console


Kivy – Console



”;


The Console tool in Kivy is similar to Inspector tool, with an additional featureprovision to add buttons and panels with the help of its addon architecture. Like the Inspector, the “kivy.modules.console” module has a command-line interface, as also an API for the tool to be used programmatically.

In the command line usage, load the module with “-m” option of Python executable.


python main.py -m console

To activate the Kivy Console, call the create_console() function from the console module with the Window and the App object as arguments. You need to place the following statement in the build() method of the App class.


from kivy.modules import console

class Demo(App):
   def build(self):
      button = Button(text="Test")
      console.create_console(Window, self)
      return button

Assume that you have developed a Kivy application with “slider.py” program that has the following interface.


Kivy Console Slider

The app has three slider controls that help in changing the color of the text above. Add the call to the create_console() function (as described above) in the build() method of slider.py file and run it. To start with the above interface will be visible. Press ctrl+E to activate the Console tool.

Following key combinations are defined −

  • “Ctrl + e” − toggle console

  • “Escape” − cancel widget lookup, then hide inspector view

  • “Up” − select the parent widget

  • “Down” − select the first child of the currently selected widget

  • “Left” − select the previous sibling

  • “Right” − select the next sibling

When the console tool is activated, it shows a bar in the middle with Select, Properties and Tree buttons on left and a FPS button on the right.


Kivy Console Fps Button

Click on the “Tree” button and you get a widget tree of the interface. Select the Slider control from the tree.


Kivy Console Tree Button

Now press the Properties button. A scrollable list of all the properties of Slider widget will be available.


Kivy Console Properties Button

Scroll down to the value property, and double click to get an editable box in which the current value is displayed, which you can change.


Kivy Console Property Value

Press escape to remove the console. You should see the Slider at the value set by you from the console.


Kivy Console Slider Value

Addons

One of the important features of the Console tool is the addon architecture which allows you to add buttons and panels to it. The addons activated by default are −

  • ConsoleAddonFps − display the FPS at the top-right.

  • ConsoleAddonSelect − activate the selection mode.

  • ConsoleAddonBreadcrumb − display the hierarchy of the current widget at the bottom.

  • ConsoleAddonWidgetTree − panel to display the widget tree of the application.

  • ConsoleAddonWidgetPanel − panel to display the properties of the selected widget.

To activate an addon, it must be added to Console.addons before the create_console is called.

About Addon

Let us add a About button the Console tool. It displays the object description including its ID in the console tool workspace in the middle.

Add the following code in slider.py, before the definition of the App class −


from kivy.modules.console import Console, ConsoleAddon, ConsoleLabel

class ConsoleAddonAbout(ConsoleAddon):
   def init(self):
      self.console.add_panel(
         "About", self.panel_activate,
         self.panel_deactivate
      )
   def panel_activate(self):
      self.console.bind(widget=self.update_content)
      self.update_content()
   def panel_deactivate(self):
      self.console.unbind(widget=self.update_content)
   def deactivate(self):
      self.panel_deactivate()
   def update_content(self, *args):
      widget = self.console.widget
      if not widget:
         return
      text = "Selected widget is: {!r}".format(widget)
      lbl = ConsoleLabel(text=text)
      self.console.set_content(lbl)
   
Console.register_addon(ConsoleAddonAbout)

If you run the slider.py script again, you get the About button added next to the Tree button. Locate the Slider from the widget tree and click on About button to show the object ID of the slider widget.


Kivy Console About Button

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *