Kivy: Make uix widgets accessible with "from kivy.uix import Button..."

Created on 25 Apr 2016  路  6Comments  路  Source: kivy/kivy

It seems almost trivial, but would it be possible to make some of the more commonly used/base widgets accessible in the kivy/uix/__init__.py module?

This would mean instead of needing to do the following:

from kivy.uix.button import Button
from kivy.uix.label import Label
...
from kivy.uix.scatter import Scatter

You only need to do the following:

from kivy.uix import Label, Button, ... , Scatter

The reason for this request is mainly to help tidy up the imports a little and means it isn't necessary to write a 30 almost identical lines when wanting to import 30 different widgets.

As far as I can tell, you would just need to import the desired classes into the kivy/uix/__init__.py module and adjust the __all__ variable like so:

# kivy/uix/__init__.py

from kivy.uix.button import Button
from kivy.uix.label import Label
...
from kivy.uix.scatter import Scatter

__all__ = ['Button', 'Label', ..., 'Scatter']

Most helpful comment

Worth mentioning that Factory provides easy import if you need it;

from kivy.factory import Factory

lbl = Factory.Label(text="Hello World")

class MyScatter(Factory.Scatter):
    pass

All 6 comments

The import syntax would be nice, but requiring all the other widgets to be present is not desired.

Duplicate of https://github.com/kivy/kivy/issues/3583.

Worth mentioning that Factory provides easy import if you need it;

from kivy.factory import Factory

lbl = Factory.Label(text="Hello World")

class MyScatter(Factory.Scatter):
    pass

Maybe that's the way how init could work? Import in init Factory and for each widget return Factory.<widget>. Or is it too harsh?

It probably would be possible to use an import hook method, though this has the disadvantage of possibly breaking the code detection of some IDEs.

Is the reason for not doing this entirely performance? If so, it would be possible (though a little untidy) to have a kivy.uix.everything module providing all the imports.

Not really performance, just making it less to write. Sometimes it reminds me java(e.g. writing the same words multiple times). It doesn't look like that if the words are short, but when you import for example screenmanager, the imports can be long.

I'm referring to the reason _not_ to have this type of import - if all the widgets are imported by __init__.py it imposes a minor perfomance penalty whenever you import any widget, since all of their files are loaded. This penalty would be quite small though, I don't know if it's important (though on Android these things can be relevant even if negligible on the desktop).

Was this page helpful?
0 / 5 - 0 ratings