0

我有一个功能:

def get_file(self):
    filechooser.open_file(on_selection=self.handle_selection, path=storagepath.get_pictures_dir())
    # some more code here
    # ...
    # ...
    requests.get(...)  # <-- crashes because it doesn't wait for the path from filechooser

我试图用time.sleep(.1)和解决它,while self.upload_path is None: pass但在功能继续之前,两者都给了我大约 10 秒的空白屏幕,这是不可接受的。

有没有其他方法可以让函数等到它有来自用户的路径?

更新:

所以根据你的回答,我想出了:

def get_file(self):
    filechooser.open_file(on_submit=self.send_to_server, on_selection=self.handle_selection, path=storagepath.get_pictures_dir())
    # some more code here
    # ...
    # ...

def handle_selection(self, selection):
    print('selected') # being fired
    return selection

def send_to_server(self, chooser_instance, selection, touch):
    upload_path = selection[0]
    print('sending to server ...') # not being fired
    requests.get(...)

在我调整代码之前:

from plyer import filechooser, storagepath


def get_file(self):
    self.upload_path = None
    filechooser.open_file(on_submit=self.send_to_server, on_selection=self.handle_selection, path=storagepath.get_pictures_dir())
    # some more code here
    # ...
    # ...

def handle_selection(self, selection):
    print('selected') # being fired
    self.upload_path = selection[0]
4

0 回答 0