|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + text_representation: |
| 5 | + extension: .md |
| 6 | + format_name: markdown |
| 7 | + format_version: '1.2' |
| 8 | + jupytext_version: 1.7.1 |
| 9 | + kernelspec: |
| 10 | + display_name: Python 3 |
| 11 | + language: python |
| 12 | + name: python3 |
| 13 | +--- |
| 14 | + |
| 15 | +# Getting started with <span style="color:#306998">Python</span><span style="color:#FFD43B">Here</span> |
| 16 | + |
| 17 | + |
| 18 | +#### Load the extension, and connect to the remote instance |
| 19 | + |
| 20 | +```python |
| 21 | +%load_ext pythonhere |
| 22 | +%connect-there |
| 23 | +``` |
| 24 | + |
| 25 | +#### Execute some code on the remote |
| 26 | + |
| 27 | +```python |
| 28 | +%%there |
| 29 | +from kivy import platform |
| 30 | +print("Hello from", platform) |
| 31 | +``` |
| 32 | + |
| 33 | +#### Use widgets |
| 34 | + |
| 35 | +```python |
| 36 | +%%there |
| 37 | +from kivy.uix.label import Label |
| 38 | + |
| 39 | +# Kivy's root widget is available via the `root` variable. |
| 40 | +root.clear_widgets() # Remove all current childrens |
| 41 | + |
| 42 | +# And add the new one |
| 43 | +widget = Label(text="Kivy", font_size="80sp") |
| 44 | +root.add_widget(widget) |
| 45 | +``` |
| 46 | + |
| 47 | +#### Objects introspection |
| 48 | + |
| 49 | +```python |
| 50 | +%%there |
| 51 | +print(root.children) |
| 52 | +print(widget.color) |
| 53 | +``` |
| 54 | + |
| 55 | + #### Modify widget properties |
| 56 | + |
| 57 | +```python |
| 58 | +%%there |
| 59 | +widget.color = [1, .5, 0, 1] |
| 60 | +widget.text += " Rocks!" |
| 61 | +``` |
| 62 | + |
| 63 | + #### Make things dynamic with [Clock](https://kivy.org/doc/stable/api-kivy.clock.html) |
| 64 | + |
| 65 | +```python |
| 66 | +%%there |
| 67 | +from kivy.clock import Clock |
| 68 | + |
| 69 | +def clock_callback(delta_time): |
| 70 | + widget.color[1] = (widget.color[1] + .1) % 1 |
| 71 | + |
| 72 | +clock = Clock.schedule_interval(clock_callback, 0.2) |
| 73 | +``` |
| 74 | + |
| 75 | +```python |
| 76 | +%%there |
| 77 | +Clock.unschedule(clock) |
| 78 | +``` |
| 79 | + |
| 80 | +#### Use platform specific features with [Plyer](https://github.com/kivy/plyer) |
| 81 | + |
| 82 | +```python |
| 83 | +%%there |
| 84 | +from plyer import tts |
| 85 | +tts.speak("yo" * 10) |
| 86 | +``` |
| 87 | + |
| 88 | +#### Declare interface with the [KV](https://kivy.org/doc/stable/guide/lang.html) language |
| 89 | + |
| 90 | +```python |
| 91 | +%%there kv |
| 92 | +Button: |
| 93 | + text: "Click me" |
| 94 | + font_size: 10 |
| 95 | + on_release: self.font_size += 1 |
| 96 | +``` |
| 97 | + |
| 98 | +#### Combine Python with KV |
| 99 | + |
| 100 | +```python |
| 101 | +%%there |
| 102 | +from plyer import vibrator |
| 103 | +from kivy.uix.button import Button |
| 104 | + |
| 105 | +class VibroClone(Button): |
| 106 | + |
| 107 | + def on_release(self): |
| 108 | + self.root.add_widget( |
| 109 | + VibroClone(root=self.root) |
| 110 | + ) |
| 111 | + vibrator.vibrate(0.05 * int(self.text)) |
| 112 | + |
| 113 | +root.clear_widgets() |
| 114 | +``` |
| 115 | + |
| 116 | +```python |
| 117 | +%%there kv |
| 118 | +#:import get_random_color kivy.utils.get_random_color |
| 119 | +#:import random random |
| 120 | + |
| 121 | +<VibroClone>: |
| 122 | + text: str(random.randint(1, 9)) |
| 123 | + color: get_random_color() |
| 124 | + background_color: get_random_color() |
| 125 | + size_hint: 1, 1 |
| 126 | + |
| 127 | +GridLayout: |
| 128 | + cols: 10 |
| 129 | + size_hint: 1, 1 |
| 130 | + VibroClone: |
| 131 | + root: root |
| 132 | +``` |
0 commit comments