본문 바로가기
PyQt5_

Working with command-line arguments

by 자동매매 2023. 3. 13.

36. Working with command-line

arguments

If you have created an application which works with specific file types — for example a video editor that opens videos, a document editor that opens document files — it can be useful to have your application open these files automatically. On all platforms, when you tell the OS to open a file with a specific application, the filename to open is passed to that application as a command-line argument.

When your application is run, the arguments passed to the application are always available in sys.argv. To open files automatically, you can check the value of sys.argv at startup and, if you find a filename in there, open it.

The following app when run will open a window with all the command line arguments received displayed.

Listing 245. further/arguments.py

from PyQt6.QtWidgets import (

QApplication,

QWidget,

QLabel,

QVBoxLayout,

)

import sys

class Window (QWidget):

def __init__ (self):

super().__init__()

layout = QVBoxLayout()

for arg in sys.argv: ①

l = QLabel(arg)

layout.addWidget(l)

self.setLayout(layout)

self.setWindowTitle("Arguments")

app = QApplication(sys.argv)

w = Window()

w.show()

app. exec ()

①sys.argv is a list of strings. All arguments are strings.

Run this app from the command line, passing in a filename (you can make anything up, we don’t load it). You can pass as many, or as few, arguments as you like.

Arguments are passed to your application as a list of str. All arguments are strings, even numeric ones. You can access any argument you like using normal

list indexing — for example `sys.argv[1] would return the 2nd argument.

Try running the script above with the following —

python arguments.py filename.mp4

This will produce the window below. Notice that when run with python the first argument is actually the Python file which is being executed.

Figure 242. The window open showing the command line arguments.

If you package your application for distribution, this may no longer be the case — the first argument may now be the file you are opening, as there is no Python file passed as an argument. This can cause problems, but a simple way around this is to use the last argument passed to your application as the filename, e.g.

if len(sys.argv) > 0 :

filename_to_open = sys.argv[- 1 ]

Alternatively, you can remove the currently executing script name if it is in the list. The currently executing Python script name is always available in file.

if __file__ in sys.argv:

sys.argv.remove(__file__)

ë It will always be in the list, unless you have packaged your app.

Below is a further example, where we accept a filename on the command line,

and then open that text file for display in a QTextEdit.

Listing 246. further/arguments_open.py

from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit

import sys

class MainWindow (QMainWindow):

def __init__ (self):

super().__init__()

self.editor = QTextEdit()

if __file__ in sys.argv: ①

sys.argv.remove(__file__)

if sys.argv: ②

filename = sys.argv[ 0 ] ③

self.open_file(filename)

self.setCentralWidget(self.editor)

self.setWindowTitle("Text viewer")

def open_file (self, fn):

with open(fn, "r") as f:

text = f.read()

self.editor.setPlainText(text)

app = QApplication(sys.argv)

w = MainWindow()

w.show()

app. exec ()

①If the script name is in sys.argv remove it.

②If there is still something in sys.argv (not empty).

③Take the first argument as the filename to open.

You can run this as follows, to view the passed in text file.

python arguments_open.py notes.txt

Packaging & Distribution

Design isn’t finished until somebody is using it.

— Brenda Laurel, PhD

There is not much fun in creating your own application if you can’t share it with other people — whether that means publishing it commercially, sharing it online or just giving it to someone you know. Sharing your apps allows other people to benefit from your hard work!

Packaging Python applications for distribution has typically been a little tricky, particularly when targeting multiple platforms (Windows, macOS and Linux). This is because of the need to bundle the source, data files, the Python runtime and all associated libraries in a way that will work reliably on the target system. Thankfully there are tools available to take care of this for you!

In this chapter we’ll walk through the process of packaging up your apps to share with other people.

'PyQt5_' 카테고리의 다른 글

Creating a Windows installer with InstallForge  (0) 2023.03.13
Packaging with PyInstaller  (0) 2023.03.13
Enums & the Qt Namespace  (0) 2023.03.13
System tray & macOS menus  (0) 2023.03.13
Working with Relative Paths  (0) 2023.03.13

댓글