ComicCreatorのGUIサンプルのテンプレートを自作のテンプレートとして実装してGUIを作ろうとしています。コードdrawingspace.kv
は簡単に理解できますが、ボタンが押されるたびに、たとえば次のように再構成できるようにしたいと考えています。
Q:drawingspace.kv
を、押されたボタンごとに異なるウィジェットで異なるレイアウトにするように構成するにはどうすればよいですか?
ComicCreatorのGUIサンプルのテンプレートを自作のテンプレートとして実装してGUIを作ろうとしています。コードdrawingspace.kv
は簡単に理解できますが、ボタンが押されるたびに、たとえば次のように再構成できるようにしたいと考えています。
Q:drawingspace.kv
を、押されたボタンごとに異なるウィジェットで異なるレイアウトにするように構成するにはどうすればよいですか?
これを行うための優れた方法は、screen を使用することです。
以前の質問でこのアプリの例を既に持っているので、画面を実装して、クラスを少し書き直すのは簡単でした。
ボタンが押されると、スクリーンマネージャーの現在の名前を、必要なスクリーンに付けた名前に設定します。
次に、各画面内、kv ファイル、または python ファイルで必要に応じてレイアウトを編集します。
ここでは、ほとんどのレイアウトを kv 言語で作成することにしました。このようにレイアウトを思いどおりに開発する方が簡単だと思うからです。必要に応じて、後でPythonに書き直すことができます。
したがって、私のpythonファイルは次のようになります。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.clock import Clock
from kivy.uix.screenmanager import Screen,ScreenManager,NoTransition
from kivy.lang import Builder
import time
Builder.load_file("kv.kv")
class MyLayout(BoxLayout):
def __init__(self,**kwargs):
super(MyLayout,self).__init__(**kwargs)
self.orientation = "vertical"
self.padding = 10
class MainScreen(Screen):
pass
class RemoveScreen(Screen):
pass
class GroupScreen(Screen):
pass
class MyLogo(BoxLayout):
your_time = StringProperty()
def __init__(self,**kwargs):
super(MyLogo,self).__init__(**kwargs)
Clock.schedule_interval(self.set_time, 0.1)
def set_time(self,dt):
self.your_time = time.strftime("%m/%d/%Y %H:%M")
class MyApp(App):
def __init__(self,**kwargs):
super(MyApp,self).__init__(**kwargs)
self.sm = ScreenManager(transition=NoTransition())
self.sm.add_widget(MainScreen(name = "main"))
self.sm.add_widget(RemoveScreen(name = "remove"))
self.sm.add_widget(GroupScreen(name = "group"))
self.sm.current = "main"
def build(self):
return self.sm
if __name__ == "__main__":
MyApp().run()
kv.kv ファイルは次のようになります。
#:kivy 1.9.1
<MyButtons@BoxLayout>:
padding: 10,10,10,0
spacing: 10
size_hint: 1,0.3
orientation: "horizontal"
Button:
text: "Clear"
on_press: app.sm.current = "main"
Button:
text: "Remove"
on_press: app.sm.current = "remove"
Button:
text: "Group"
on_press: app.sm.current = "group"
Button:
text: "Color"
Button:
text: "Gestures"
<MyLogo>:
spacing: 10
padding: 10,10,10,0
orientation: "horizontal"
BoxLayout:
orientation: "vertical"
size_hint: 0.3,1
canvas:
Rectangle:
pos: self.pos
size: self.size
AsyncImage
source: 'http://lmsotfy.com/so.png'
Label:
size_hint: 1,0.3
text: root.your_time
color: [0,0,0,1]
Label:
size_hint: 1,0.3
text: "NYC, New York, USA"
color: [0,0,0,1]
<MainScreen>:
MyLayout:
MyLogo:
#Button:
# text: "main"
MyButtons:
#buttons
BoxLayout:
padding: 10,10,10,10
size_hint: 1,0.3
Button:
text: "Total figures: 1 Kivy Started"
<RemoveScreen>:
MyLayout:
MyLogo:
BoxLayout:
orientation: "horizontal"
Label:
font_size: "40sp"
text: "Remove"
Button:
font_size: "20sp"
text: "Remove this or something"
MyButtons:
#buttons
BoxLayout:
padding: 10,10,10,10
size_hint: 1,0.3
Button:
text: "Total figures: 1 Kivy Started"
<GroupScreen>:
MyLayout:
MyLogo:
BoxLayout:
orientation: "vertical"
Label:
font_size: "40sp"
text: "Group"
Button:
font_size: "20sp"
text: "Something groups stuff"
MyButtons:
#buttons
BoxLayout:
padding: 10,10,10,10
size_hint: 1,0.3
Button:
text: "Total figures: 1 Kivy Started"
レイアウト フレームはスクリーン マネージャであり、各レイアウトはスクリーンである必要があります。その後、ボタンを押すと画面遷移がトリガーされます。これを行う方法がわからない場合は、ここでチュートリアルを見ることもできますが、ドキュメントで十分です。