doc:appunti:prog:kivy
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
doc:appunti:prog:kivy [2019/02/25 10:44] – niccolo | doc:appunti:prog:kivy [2023/10/07 19:53] (current) – [Logging] niccolo | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Appunti Kivy ====== | ====== Appunti Kivy ====== | ||
+ | |||
+ | ===== Python Code and KV Language ===== | ||
This is the **kivyexample.py** file: | This is the **kivyexample.py** file: | ||
Line 48: | Line 50: | ||
{{.: | {{.: | ||
+ | |||
+ | We used a KV file to define the graphic interface of our App, but **it is possible to do that using only Python code**, thus having only a single file. But doing as seen above has several advantages. First of all you can try to keep the **graphical presentation** and the **code logic** as much **separate** as possibile. Another advantage is that resizing the app's window, will resize all its contents automatically (if you are smart enough not to use absolute values); otherwise you have to bind the window-resize event to a function which should adjust each widget size and position. | ||
+ | |||
+ | ===== Using the Widget Class ===== | ||
+ | |||
+ | In general our App will return a class derived from the more generic **Widget** class, not the // | ||
+ | |||
+ | < | ||
+ | < | ||
+ | FloatLayout: | ||
+ | size: root.size | ||
+ | MyButton: | ||
+ | text: "Pos 0, 0" | ||
+ | pos_hint: {' | ||
+ | </ | ||
+ | |||
+ | We requested that the FloatLayout expands to all the parent (root) **size**, so the example will give the same output as in the previous paragraph. | ||
+ | |||
+ | ===== A Real App With an ActionBar ===== | ||
+ | |||
+ | **kivywindowexample.py** | ||
+ | |||
+ | <code python> | ||
+ | # | ||
+ | from kivy.app import App | ||
+ | # | ||
+ | from kivy.uix.widget import Widget | ||
+ | |||
+ | class MyWidget(Widget): | ||
+ | pass | ||
+ | |||
+ | class KivyWindowExampleApp(App): | ||
+ | def build(self): | ||
+ | return MyWidget() | ||
+ | |||
+ | if __name__ == " | ||
+ | KivyWindowExampleApp().run() | ||
+ | </ | ||
+ | |||
+ | **kivywindowexample.kv** | ||
+ | |||
+ | < | ||
+ | < | ||
+ | font_size: 28 | ||
+ | color: 0,1,0,1 | ||
+ | size_hint: 0.3, 0.2 | ||
+ | |||
+ | < | ||
+ | BoxLayout: | ||
+ | size: root.size | ||
+ | orientation: | ||
+ | ActionBar: | ||
+ | pos_hint: {' | ||
+ | ActionView: | ||
+ | use_separator: | ||
+ | ActionPrevious: | ||
+ | title: " | ||
+ | with_previous: | ||
+ | ActionOverflow: | ||
+ | ActionButton: | ||
+ | text: " | ||
+ | ActionButton: | ||
+ | text: " | ||
+ | ActionButton: | ||
+ | text: "New Game" | ||
+ | FloatLayout: | ||
+ | MyButton: | ||
+ | text: "Pos 0, 0" | ||
+ | pos_hint: {' | ||
+ | MyButton: | ||
+ | text: " | ||
+ | pos_hint: {' | ||
+ | MyButton: | ||
+ | text: "Pos right-top" | ||
+ | pos_hint: {' | ||
+ | MyButton: | ||
+ | text: "Pos 0.5, 1.0" | ||
+ | pos_hint: {' | ||
+ | </ | ||
+ | |||
+ | {{.: | ||
+ | |||
+ | |||
+ | ===== Logging ===== | ||
+ | |||
+ | Like a plain Python program, it is possible for a Kivy app to produce some logging using the Kivy logger. The output will go, by default, to a file created into the app home directory, into the **files/ | ||
+ | |||
+ | <code python> | ||
+ | from kivy.logger import Logger, LOG_LEVELS | ||
+ | # Set the loglevel. The Android log file will be create into | ||
+ | # [app_home]/ | ||
+ | Logger.setLevel(LOG_LEVELS[' | ||
+ | Logger.info(' | ||
+ | </ | ||
+ | |||
+ | Without root privileges it is not possibile for another Android app (e.g. a file browser) to access the log directory. The Kivy **app home** will be into the adopted SD card, under something like this: | ||
+ | |||
+ | < | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | In older Android versions (e.g. Android 8) the app home directory will be something like: | ||
+ | |||
+ | < | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | Every time that the Kivy environment is initializated, | ||
+ | |||
+ | |||
+ |
doc/appunti/prog/kivy.1551091486.txt.gz · Last modified: 2019/02/25 10:44 by niccolo