简体   繁体   中英

add drag and drop support to a python script

hi I have a Python script that does something simple. it simply convert file.txt to file2.txt here a very simple script for show you what i want

with open("file.txt", encoding="utf-16-le") as file_in:

with open("file2.txt", "w", encoding="utf-8") as output:

I want to add a capability to this script, when i drag file-text.txt on this script, script first convert file then output it with name file-text- out .txt

You will need to write a Python program with a GUI. Your program must open a graphical window. To write a Python program with a graphical window, you need a GUI framework.

Many GUI framework for Python exist. But not all of them support drag & drop easily.

One possible GUI framework is wxPython .

I copied the code for class MyFileDropTarget that you can find in the link above. I was able to start the program. When started, the program opens this window:

在此处输入图像描述

I was also able to drag and drop a file from my desktop into this window. After I dragged it, the text "1 file(s) dropped at 86,55: /home/lydia/testfile.txt" appeared in the window. This means that the program understood the event and was able to respond.

When you got this far, I suggest you continue with carefully studying the getting-started page from wxPython.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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