简体   繁体   English

Kivy中的on_drop_file function for python传5个arguments,但我的drop function中只允许3个arguments

[英]The on_drop_file function in Kivy for python passes 5 arguments, but only 3 arguments are allowed in my drop function

I was following this - Kivy: drag n drop, get file path tutorial from stackoverflow in order to implement a drag and drop file feature in my kivy gui application.我正在关注这个 - Kivy:拖放,从 stackoverflow 获取文件路径教程,以便在我的 kivy gui 应用程序中实现拖放文件功能。 However, when I ran the example code:但是,当我运行示例代码时:

from kivy.app import App
from kivy.core.window import Window


class WindowFileDropExampleApp(App):
    def build(self):
        Window.bind(on_dropfile=self._on_file_drop)
        return

    def _on_file_drop(self, window, file_path):
        print(file_path)
        return

if __name__ == '__main__':
    WindowFileDropExampleApp().run()

I got a message saying that the on_dropfile feature was deprecated and that I should use the on_drop_file feature instead.我收到一条消息说 on_dropfile 功能已被弃用,我应该改用 on_drop_file 功能。 Upon changing it to on_drop_file, in the following code:在将其更改为 on_drop_file 后,在以下代码中:

import kivy
kivy.require('1.10.0')

from kivy.app import App
from kivy.uix.button import Label
from kivy.core.window import Window


class Gui(App):

    def build(self):
        Window.bind(on_drop_file=self._on_file_drop)
        return Label(text = "Drag and Drop File here")

    def _on_file_drop(self, window, file_path):
        print(file_path)
        return


if __name__ == '__main__':
    drop = Gui()

    drop.run()

I got the following error:我收到以下错误:

 TypeError: Gui._on_file_drop() takes 3 positional arguments but 5 were given

I can't seem to find what the 5 positional arguments that I'm supposed to include are.我似乎找不到我应该包含的 5 位置 arguments 是什么。 How should my _on_file_drop() function be modified to make this work?应该如何修改我的_on_file_drop() function 以使其工作?

This issue can be resolved by just creating arbitrary x and y arguments in the on_file_drop() function, which would look like the following:这个问题可以通过在on_file_drop() function 中创建任意 x 和 y arguments 来解决,如下所示:

 def _on_file_drop(self, window, file_path, x, y):
        print(file_path)
        return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM