|
| 1 | +from kivy.app import App |
| 2 | +from kivy.graphics import Line, Color, Rectangle, Ellipse |
| 3 | +from kivy.metrics import dp |
| 4 | +from kivy.properties import StringProperty, BooleanProperty, Clock |
| 5 | +from kivy.uix.boxlayout import BoxLayout |
| 6 | +from kivy.uix.anchorlayout import AnchorLayout |
| 7 | +from kivy.uix.gridlayout import GridLayout |
| 8 | +from kivy.uix.stacklayout import StackLayout |
| 9 | +from kivy.uix.button import Button |
| 10 | +from kivy.uix.widget import Widget |
| 11 | + |
| 12 | + |
| 13 | +class WidgetsExample(GridLayout): |
| 14 | + count = 1 |
| 15 | + count_enabled = BooleanProperty(False) |
| 16 | + my_text = StringProperty("1") |
| 17 | + # slider_value_text = StringProperty("Value") |
| 18 | + text_input_str = StringProperty("foo") |
| 19 | + |
| 20 | + def on_button_click(self): |
| 21 | + print("Button clicked!") |
| 22 | + |
| 23 | + if self.count_enabled: |
| 24 | + self.count += 1 |
| 25 | + self.my_text = str(self.count) |
| 26 | + |
| 27 | + def on_toggle_button_state(self, widget): |
| 28 | + print("toggle state: " + widget.state) |
| 29 | + if widget.state == "normal": |
| 30 | + # OFF |
| 31 | + widget.text = "OFF" |
| 32 | + self.count_enabled = False |
| 33 | + else: |
| 34 | + # ON |
| 35 | + widget.text = "ON" |
| 36 | + self.count_enabled = True |
| 37 | + |
| 38 | + def on_switch_active(self, widget): |
| 39 | + print("Switch: " + str(widget.active)) |
| 40 | + |
| 41 | + # def on_slider_value(self, widget): |
| 42 | + # print("Slider Value: " + str(int(widget.value))) |
| 43 | + # self.slider_value_text = str(int(widget.value)) |
| 44 | + |
| 45 | + def on_text_validate(self, widget): |
| 46 | + self.text_input_str = widget.text |
| 47 | + |
| 48 | + |
| 49 | +class StackLayoutExample(StackLayout): |
| 50 | + def __init__(self, **kwargs): |
| 51 | + super().__init__(**kwargs) |
| 52 | + # self.orientation = "rl-bt" |
| 53 | + for i in range(0, 100): |
| 54 | + size = dp(100) + i*10 |
| 55 | + size = dp(100) |
| 56 | + # b = Button(text=str(i+1), size_hint=(None, None), size=(dp(100), dp(100))) |
| 57 | + b = Button(text=str(i + 1), size_hint=(None, None), size=(size, size)) |
| 58 | + self.add_widget(b) |
| 59 | + |
| 60 | + |
| 61 | +# class GridLayoutExample(GridLayout): |
| 62 | +# pass |
| 63 | + |
| 64 | + |
| 65 | +class AnchorLayoutExample(AnchorLayout): |
| 66 | + pass |
| 67 | + |
| 68 | + |
| 69 | +class BoxLayoutExample(BoxLayout): |
| 70 | + pass |
| 71 | + |
| 72 | + |
| 73 | +"""" |
| 74 | + def __init__(self, **kwargs): |
| 75 | + super().__init__(**kwargs) |
| 76 | +
|
| 77 | + self.orientation = "vertical" |
| 78 | +
|
| 79 | + b1 = Button(text="A") |
| 80 | + b2 = Button(text="B") |
| 81 | + b3 = Button(text="C") |
| 82 | +
|
| 83 | + self.add_widget(b1) |
| 84 | + self.add_widget(b2) |
| 85 | + self.add_widget(b3) |
| 86 | +""" |
| 87 | + |
| 88 | + |
| 89 | +class MainWidget(Widget): |
| 90 | + pass |
| 91 | + |
| 92 | + |
| 93 | +class TheLabApp(App): |
| 94 | + pass |
| 95 | + |
| 96 | + |
| 97 | +class CanvasExample1(Widget): |
| 98 | + pass |
| 99 | + |
| 100 | + |
| 101 | +class CanvasExample2(Widget): |
| 102 | + pass |
| 103 | + |
| 104 | + |
| 105 | +class CanvasExample3(Widget): |
| 106 | + pass |
| 107 | + |
| 108 | + |
| 109 | +class CanvasFromTheCode(Widget): |
| 110 | + def __init__(self, **kwargs): |
| 111 | + super().__init__(**kwargs) |
| 112 | + with self.canvas: |
| 113 | + Line(points=(100, 100, 400, 500), width=2) |
| 114 | + Color(0, 1, 0) |
| 115 | + Line(circle=(400, 200, 80), width=2) |
| 116 | + Line(rectangle=(600, 200, 150, 100), width=5) |
| 117 | + Color(1, 1, 0) |
| 118 | + self.rect = Rectangle(pos=(600, 400), size=(150, 100)) |
| 119 | + |
| 120 | + def on_button_a_click(self): |
| 121 | + # print("foo") |
| 122 | + x, y = self.rect.pos |
| 123 | + w, h = self.rect.size |
| 124 | + inc = dp(10) |
| 125 | + |
| 126 | + diff = self.width - (x+w) |
| 127 | + |
| 128 | + # print("diff= ", int(diff)) |
| 129 | + |
| 130 | + if diff < inc: |
| 131 | + inc = diff |
| 132 | + |
| 133 | + x += inc |
| 134 | + self.rect.pos = (x, y) |
| 135 | + |
| 136 | + |
| 137 | +class CanvasExample4(Widget): |
| 138 | + def __init__(self, **kwargs): |
| 139 | + super().__init__(**kwargs) |
| 140 | + self.ball_size = dp(50) |
| 141 | + self.vx = dp(3) # vx = velocity of x |
| 142 | + self.vy = dp(4) # vy = velocity of y |
| 143 | + with self.canvas: |
| 144 | + self.ball = Ellipse(pos= self.center, size= (self.ball_size, self.ball_size)) |
| 145 | + Clock.schedule_interval(self.update, 1/60) |
| 146 | + |
| 147 | + def on_size(self, *args): |
| 148 | + # print("on size: " + str(self.width) + ", " + str(self.height)) |
| 149 | + # self.ball.pos = self.center |
| 150 | + self.ball.pos = (self.center_x - self.ball_size/2, self.center_y - self.ball_size/2) |
| 151 | + |
| 152 | + def update(self, dt): # dt = delta time |
| 153 | + # print("update") |
| 154 | + x, y = self.ball.pos |
| 155 | + |
| 156 | + x += self.vx |
| 157 | + y += self.vy |
| 158 | + |
| 159 | + # self.ball_size / self.width |
| 160 | + # self.vx = - self.vx # revert |
| 161 | + if y + self.ball_size > self.height: |
| 162 | + y = self.height - self.ball_size |
| 163 | + self.vy = -self.vy # invert |
| 164 | + |
| 165 | + if x + self.ball_size > self.width: |
| 166 | + x = self.width - self.ball_size |
| 167 | + self.vx = -self.vx # invert |
| 168 | + |
| 169 | + if y < 0: |
| 170 | + y = 0 |
| 171 | + self.vy = -self.vy |
| 172 | + |
| 173 | + if x < 0: |
| 174 | + x = 0 |
| 175 | + self.vx = -self.vx |
| 176 | + |
| 177 | + self.ball.pos = (x, y) |
| 178 | + |
| 179 | + |
| 180 | +class CanvasExample5(Widget): |
| 181 | + pass |
| 182 | + |
| 183 | + |
| 184 | +class CanvasExample6(BoxLayout): |
| 185 | + pass |
| 186 | + |
| 187 | + |
| 188 | +TheLabApp().run() |
0 commit comments