ウィジェット コマンド add_widget と clear_widgets を使用しているときに、自分のウィジェットが画面の左下隅で信じられないほど小さくなっています。
ここですべての手順を説明しますので、ご容赦ください。
あるウィジェットから別のウィジェットに切り替えるシステムが整っています。「ウィジェット」は、基本的にフォーム全体、ベースキャンバス、およびレイアウトです。
私が理解しているように、これらには基本レイアウトが必要です。最初にルート要素を配置し、次にこのルートで UI Manager を初期化します。
class MyApp(App):
rootForm = BoxLayout()
ui = UserInterface(rootForm)
return rootForm
私の UI は、ウィジェットの切り替えを処理します。作成時にすべてのウィジェットをリストに保持します。また、コールバックの目的で、ユーザー インターフェイスへの参照を自身に与えます。
class UserInterface():
__WidgetDictionary = {}
__RootWidget = None
def __init__(self, inRootWidget):
self.__RootWidget = inRootWidget # Keep track of our root
# Generate two forms to be swapped in/out
self.__WidgetDictionary["form1"] = FirstWidget()
self.__WidgetDictionary["form1"].SetUIreference(self)
self.__WidgetDictionary["form2"] = SecondWidget()
self.__WidgetDictionary["form2"].SetUIReference(self)
self.ChangeWidget("MainMenu")
ここでの真の心臓部である ChangeWidget は、ルート ウィジェットを一掃し、新しいウィジェットを追加します。
def ChangeWidget(self, inWidgetName):
self.__RootWidget.clear_widgets() # Clear out the widgets
self.__RootWidget.add_widget( self.__WidgetDictionary[inWidgetName] ) # Add our new widget
これにより、フォームがボタン付きで正常に表示されます。ボタンがクリックされると、フォームを Form1 から Form2 に変更するためのコールバックがアタッチされています。
#FirstWidget.py
class FirstWidget(BoxLayout):
__UserInterfaceReference = None
def SetUIReference(self, inUserInterface):
self.__UserInterfaceReference = inUserInterface
def ChangeWidget(self, inWidgetName):
self.__UserInterfaceReference.ChangeWidget(inWidgetName)
対応する .kv ファイル
#FirstWidget.kv
<FirstWidget>:
BoxLayout:
orientation: 'vertical'
padding: 50
spacing: 10
Button:
on_release: root.ChangeWidget('form2')
ボタンをクリックすると、エラーが発生します。Form1 と Form2 はほぼ同じです。Form2 には、form2 よりもボタンが 1 つ多くあります。Form1 と Form2 のコードは、クラス名を除いてまったく同じです。繰り返しますが、独自の環境では、form2 は完璧に見えます。
バグのスクリーンショットは次のとおりです。
編集: 当分の間、スクリーン マネージャーの使用に切り替えました: http://kivy.org/docs/api-kivy.uix.screenmanager.html