我正在寻找一种方法,可通过单击主应用程序屏幕上的按钮来创建一个带有文件浏览器的弹出框。以下代码段是在主应用程序中单击“上传”按钮时调用的类。它将使用文件浏览器呈现弹出窗口,但是on_ * kwargs不会运行相应的方法。
class UploadPopup:
def __init__(self, short_text='heading'):
browser = FileBrowser(select_string='Select', cancel_state='down')
browser.bind(on_success=self._fbrowser_success,
on_canceled=self._fbrowser_canceled,
on_submit=self._fbrowser_submit)
self.popup = Popup(
title=short_text,
content=browser, size_hint=(0.9, 0.9),
auto_dismiss=False
)
self.popup.open()
def _fbrowser_canceled(self, instance):
print('cancelled, Close self.')
self.popup.dismiss()
def _fbrowser_success(self, instance):
print(instance.selection)
self.popup.dismiss()
def _fbrowser_submit(self, instance):
print(instance.selection)
self.popup.open()
任何想法?
答案 0 :(得分:1)
无论您在哪里调用UploadPopup()
,都需要保存对其的引用,以免收集垃圾。例如,您可能想在主应用中执行self.pop = UploadPopup()
。
此外,在事件处理方法中,您可能希望将self.popup.open()
替换为self.popup.dismiss()
。
答案 1 :(得分:0)
我已经接受了一个答案,但想在此基础上再扩展一点。 @ john-anderson是正确的,因为垃圾回收正在收集实例化。我原本是这样做的...
class MainScreen(Screen):
...
@staticmethod
def upload(self):
this = UploadPopup()
...
class UploadPopup():
....
see original post
....
为了解决此问题,我必须在MainScreen类的init方法中实例化一个方法,将一个open方法添加到UpdatePopup类,并在单击按钮时调用该方法。
class MainScreen(Screen):
def __init__(self):
self.upload_popup = UploadPopup()
...
@staticmethod
def upload(self):
self.upload_popup.start()
...
class UploadPopup:
def __init__(self, short_text='heading'):
browser = FileBrowser(select_string='Select', cancel_state='down')
browser.bind(on_success=self._fbrowser_success,
on_canceled=self._fbrowser_canceled,
on_submit=self._fbrowser_submit)
self.popup = Popup(
title=short_text,
content=browser, size_hint=(0.9, 0.9),
auto_dismiss=False
)
def start(self):
self.popup.open()
....