Using command line arguments to open files with PyQt5 apps -- Windows file associations

Heads up! You've already completed this tutorial.

qinhong_lai | 2020-05-28 07:09:37 UTC | #1

I developed a simple video player and packed it with pyinstaller. After installation, if I click on the .MP4 file, how to open it with my player? I try to open the. MP4 file. First, I want to get the file name and path, but I don't know how to do it. Can you help me think about it? 35|89x101


Luca | 2020-05-27 10:46:50 UTC | #2

I'm not sure that I completely understood what you want to do.

However, if you are on windows and you want to add your program to the open with list, try to read this:

https://docs.microsoft.com/it-it/visualstudio/extensibility/specifying-file-handlers-for-file-name-extensions?view=vs-2019

Of course, in your case you should use the .mp4 extension instead of the one in the article.

Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

If you don't want to manually change the windows registry try to see if the following utility could be helpful:

https://defaultprogramseditor.com/


martin | 2020-05-28 07:09:43 UTC | #3

Hey @qinhong_lai welcome to the forum! As @Luca says you can set your application as the "file handler" for a given file type.

You can set this up by right-clicking on a file in Explorer, selecting Open with... and then "Choose another app".

In the window that opens you'll see applications that are installed on your computer. If you don't see your application here, keep scrolling down and you'll see "Look for another app on this PC" and then you can select your app by EXE file.

When Windows tries to open the file with your application, it will pass the filename as an argument to your app -- the filename will be available in sys.argv. The following app example will display all the command line arguments when run.

python
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout

import sys


class Window(QWidget):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()

        for arg in sys.argv:  # <1> sys.argv is a list of strings.
            l = QLabel(arg)
            layout.addWidget(l)

        self.setLayout(layout)
        self.setWindowTitle("Arguments")


app = QApplication(sys.argv)
w = Window()
w.show()

app.exec_()

If you run this with python file.py <filename to open> the file.py will be the first argument in sys.argv. What happens in a packaged app will depend on how it's packaged.

For example,

python
python arguments.py filename.mp4

Gives the following window

arguments|150x104

For opening a file, you could also use sys.argv[-1] to get the last argument (if you're not passing anything else). Or alternatively, remove the current script filename from the list using the following

python
if __file__ in sys.argv:
    sys.argv.remove(__file__)

qinhong_lai | 2020-05-28 05:59:38 UTC | #4

Thank you. You have successfully solved this problem and obtained the file path and name. Now I have another problem

python
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()

        for arg in sys.argv:  # <1> sys.argv is a list of strings.
            l = QLabel(arg)
            layout.addWidget(l)

        self.setWindowIcon(QIcon('resource/logo.ico'))
        self.setLayout(layout)
        self.setWindowTitle("Arguments")

app = QApplication(sys.argv)
w = Window()
w.show()
app.exec_()

I want to add a program icon self.setWindowIcon(QIcon('resource/logo.ico')) It can be displayed normally

But I use pyinstaller to package and generate exe pyinstaller -w test20.py

When the .MP4 video file on the desktop of the computer is opened through test20.exe, it will not be normal displayed. That's why I can only copy one picture to the desktop,This is not the way to do it 43|404x91

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Using command line arguments to open files with PyQt5 apps -- Windows file associations was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.