[英]Open multiple files from file managers
I've built a (Linux) GUI application that can be launched from a terminal and accepts an undefined number of files as arguments. 我已经构建了一个(Linux)GUI应用程序,该应用程序可以从终端启动,并接受未定义数量的文件作为参数。 The app reads sys.argv and lists the name of these files in a QListWidget.
该应用程序读取sys.argv并在QListWidget中列出这些文件的名称。
The code is something like: 代码类似于:
import sys
from PyQt4.QtGui import QApplication, QMainWindow, QCoreApplication
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
# parse command line arguments
for i in QCoreApplication.argv()[1:]:
...
def main():
app = QApplication(sys.argv)
...
What I want to do is to be able to select multiple files from a file manager and open them with my app through the "Open with..." option provided by file managers. 我要做的是能够从文件管理器中选择多个文件,并通过文件管理器提供的“打开方式...”选项使用我的应用程序打开它们。 How this can be achieved?
如何做到这一点?
With the current code, when I try it only one of the selected files is shown on the QListWidget. 使用当前代码,当我尝试使用它时,QListWidget上仅显示所选文件之一。
Edit: 编辑:
It finally seems that it depends to the file manager. 最终看来,这取决于文件管理器。 I tried with a few file managers and...
我尝试了一些文件管理器,然后...
pcmanfm: It only opens one of the selected files. pcmanfm:它仅打开所选文件之一。
spacefm: Works properly! spacefm:正常工作!
dolphin: It opens each file to a different instance of my program. 海豚:它将每个文件打开到我程序的不同实例。 If I select 3 files it will open my app 3 times, one for each file.
如果我选择3个文件,它将打开我的应用3次,每个文件一个。
nautilus: I didn't manage to open any files with it. nautilus:我没有用它打开任何文件。 My program is not listed in the suggested applications and I didn't find any way to do it.
我的程序未在建议的应用程序中列出,我找不到任何方法。
There's not really enough information to give a definite answer, but... 确实没有足够的信息来给出明确的答案,但是...
First, have you checked that a print sys.argv
at the top of the code looks like you were expecting? 首先,您是否检查过代码顶部的
print sys.argv
看起来像您期望的那样?
If so, does it work if you change the line... 如果是这样,如果您更改线路,它是否起作用...
for i in QCoreApplication.argv()[1:]:
...to... ...至...
for i in sys.argv[1:]:
For debugging purposes, you might also like to include the line... 出于调试目的,您可能还希望包括该行...
assert QCoreApplication.argv()[1:] == sys.argv[1:]
...just before you start the for-loop. ...就在开始循环之前。
使用QFileDialog: 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.