回答編集履歴
2
widgetの派生classを作らないcodeを追記
answer
CHANGED
@@ -14,4 +14,57 @@
|
|
14
14
|
|
15
15
|
といった風に位置を設定してあげればいいです。(本当はここで座標変換もしておいたほうが変更に強いcodeになって良いのですが、話がややこしくなるのでしてません)。
|
16
16
|
|
17
|
-
それとwidgetの多重継承をしている理由は何ですか?`class MyBoxLayout(BoxLayout,FloatLayout):`は多分`class MyBoxLayout(BoxLayout)`と同じですし、`MyWidget`に関しても他のwidgetと多重継承して使うのが目的なら`MyWidget`自体は何も継承しなくていいです。(`class MyWidget:`だけで良い。Kivyの慣例に従うなら`XXXBehavior`という名前にしておくと読み手にとって分かりやすいです)。
|
17
|
+
それとwidgetの多重継承をしている理由は何ですか?`class MyBoxLayout(BoxLayout,FloatLayout):`は多分`class MyBoxLayout(BoxLayout)`と同じですし、`MyWidget`に関しても他のwidgetと多重継承して使うのが目的なら`MyWidget`自体は何も継承しなくていいです。(`class MyWidget:`だけで良い。Kivyの慣例に従うなら`XXXBehavior`という名前にしておくと読み手にとって分かりやすいです)。
|
18
|
+
|
19
|
+
# 追記: widgetの派生classを作らないやり方
|
20
|
+
|
21
|
+
### 方法その1: Kv言語無し
|
22
|
+
|
23
|
+
```python3
|
24
|
+
from kivy.app import App
|
25
|
+
|
26
|
+
|
27
|
+
class TameshiApp(App):
|
28
|
+
def build(self):
|
29
|
+
from kivy.factory import Factory as F
|
30
|
+
root = F.FloatLayout()
|
31
|
+
widget = F.Widget()
|
32
|
+
widget.bind(on_touch_down=lambda w, t: root.add_widget(
|
33
|
+
F.Button(text='Success', size_hint=(.2, .2, ), pos=t.pos)))
|
34
|
+
root.add_widget(widget)
|
35
|
+
root.add_widget(F.Label(text='Label', pos=(100, 100)))
|
36
|
+
root.add_widget(F.Button(text='Button', pos=(200, 200), size_hint=(.2, .2)))
|
37
|
+
return root
|
38
|
+
|
39
|
+
TameshiApp().run()
|
40
|
+
```
|
41
|
+
|
42
|
+
### 方法その2: Kv言語有り
|
43
|
+
|
44
|
+
```python3
|
45
|
+
from kivy.app import App
|
46
|
+
from kivy.lang import Builder
|
47
|
+
|
48
|
+
|
49
|
+
KV_CODE = '''
|
50
|
+
#:import Button kivy.uix.button.Button
|
51
|
+
|
52
|
+
FloatLayout:
|
53
|
+
Widget:
|
54
|
+
on_touch_down: root.add_widget(Button(text='Success', size_hint=(.2, .2, ), pos=args[1].pos))
|
55
|
+
Label:
|
56
|
+
text: 'Label'
|
57
|
+
pos: 100, 100
|
58
|
+
Button:
|
59
|
+
text: 'Button'
|
60
|
+
pos: 200, 200
|
61
|
+
size_hint: .2, .2
|
62
|
+
'''
|
63
|
+
|
64
|
+
|
65
|
+
class TameshiApp(App):
|
66
|
+
def build(self):
|
67
|
+
return Builder.load_string(KV_CODE)
|
68
|
+
|
69
|
+
TameshiApp().run()
|
70
|
+
```
|
1
説明の追加
answer
CHANGED
@@ -1,1 +1,17 @@
|
|
1
|
-
`MakeButton`を定義するcodeはあっても作るcodeが何処にも無いです。
|
1
|
+
`MakeButton`を定義するcodeはあっても作るcodeが何処にも無いです。作りたい時に以下のように作って参照を持っておいて
|
2
|
+
|
3
|
+
```python
|
4
|
+
make_button = MakeButton(...)
|
5
|
+
親.add_widget(make_button)
|
6
|
+
```
|
7
|
+
|
8
|
+
その後
|
9
|
+
|
10
|
+
```
|
11
|
+
def on_touch_down(self, touch):
|
12
|
+
make_button.pos = touch.pos
|
13
|
+
```
|
14
|
+
|
15
|
+
といった風に位置を設定してあげればいいです。(本当はここで座標変換もしておいたほうが変更に強いcodeになって良いのですが、話がややこしくなるのでしてません)。
|
16
|
+
|
17
|
+
それとwidgetの多重継承をしている理由は何ですか?`class MyBoxLayout(BoxLayout,FloatLayout):`は多分`class MyBoxLayout(BoxLayout)`と同じですし、`MyWidget`に関しても他のwidgetと多重継承して使うのが目的なら`MyWidget`自体は何も継承しなくていいです。(`class MyWidget:`だけで良い。Kivyの慣例に従うなら`XXXBehavior`という名前にしておくと読み手にとって分かりやすいです)。
|