1
TextInput
フィールドにあなたの名前と年齢を尋ねる簡単なアプリがあります。 保存ボタンをクリックするとPopup
が開き、TextInput
の名前と年齢をファイルに保存できます。KivyがTextInputをポップアップから取得
質問: Popup
が既に開いているときに、名前と年齢にアクセスするにはどうすればよいですか? 今はPopup
を開く前にTextInput
のデータを辞書に保存しています。 この回避策は機能しますが、最も確かにこれ以上エレガントな解決策があります:
class SaveDialog(Popup):
def redirect(self, path, filename):
RootWidget().saveJson(path, filename)
def cancel(self):
self.dismiss()
class RootWidget(Widget):
data = {}
def show_save(self):
self.data['name'] = self.ids.text_name.text
self.data['age'] = self.ids.text_age.text
SaveDialog().open()
def saveFile(self, path, filename):
with open(path + '/' + filename, 'w') as f:
json.dump(self.data, f)
SaveDialog().cancel()
ポップアップからデータを取得してメインアプリケーションにアクセスする方法はありますか? – user2067030
@ user2067030メインウィジェットクラスでは、 'self.save_popup'はポップアップオブジェクトです。したがって、そのデータにアクセスするには、その属性 'self.save_popup.whatever_data_you_save_in_there' – EL3PHANTEN