0
私はおそらく単純なタスクを解決するためにstruglingですので、私は比較的新しいPythonとKivyの新しいです。 Button1とButton2の2つのボタンを持つメイン画面が必要です。ボタンを押すと、もう一度ボタンの数を示す2番目の画面が表示されます。 2番目の画面のボタンの数は動的ですが、わかりやすくわかります。Python Kivy:ループ内に複数のウィジェットを追加する方法
のpythonコード:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
buttons = {}
buttons['Button 1'] = ('A Button 1', 'A Button 2')
buttons['Button 2'] = ('B Button 1', 'B Button 2', 'B Button 3')
class SelectButton(BoxLayout):
def show_buttons(self, button):
self.clear_widgets() # I guess I need this
#Here question comes: how to add button widgets buttons[button]?
#Shall I do this in the loop in Python code or in .kv file?
#for item in buttons[button]:
# print (item)
class TestApp(App):
pass
if __name__ == '__main__':
TestApp().run()
.kvファイル:
SelectButton:
<SelectButton>:
orientation: "vertical"
Label:
size_hint_y: 0.15
text: "Select Button"
BoxLayout:
Button:
text: "Button 1"
on_press: root.show_buttons(self.text)
Button:
text: "Button 2"
on_press: root.show_buttons(self.text)
あなたの問題は何を使用して押されたときにそれらに切り替えることをお勧めですか? –
問題は、追加するボタンの数が動的である場合、すべてのグラフィカル要素を定義するはずの.kvファイルを使ってどのように行うことができるかです。 – Agenobarb
私はKivyの専門家ではありません。私はkivy言語を意図的に使用していませんでした...しかし、Pythonコードで定義する以外に選択肢がないと思います。正直言って、.kvで動的に作成できますか?Pythonではおそらくもっと簡単でしょう。 –