Trying to open a file in my Gtk4/PyGObject app that was provided using the command line

Hello!

I am trying to get my Gtk4/PyGObject application to open a file on startup when a filename is provided on the command-line:

python3 main.py filename.txt

But whatever guide I am trying to follow, I always get an error:

(main.py:1800): GLib-GIO-CRITICAL **: 15:27:53.914: This application can not open files.

I understood from the documentation that I need to enable this:

Which I did when I made the same changes as this file has:

https://fedorarules.blogspot.com/2013/09/how-to-handle-command-line-options-in.html

I know it’s Gtk3, not Gtk4 like I have, but I have found several pieces of code for this and I cannot get it to work. Is there an example for this?

Thanks in advance =)

I got it somewhat working this morning. I followed the example here:

But when I try to print the arguments in the main window, I cannot access the values:


class MainWindow(Gtk.ApplicationWindow):

    """Command line argument test app."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        print("args:", args)
        print("kwargs:", kwargs)

Prints out:

args: ()
kwargs: {‘application’: <main.cliTestApp object at 0x7fa73cd42fc0 (main+cliTestApp at 0x55ba45f3c970)>}

How can I access the argument values in the main window?

Hello again!

I found some more information about this again. Here it was said:

Using HANDLES_OPEN will do the work of simply taking file arguments for you and let you handle it in Gio.Application.do_open().

So there is some functionality for this built-in it seems. I clicked on the link and found this:

do_open(files: list[File], hint: str) → None

Opens the given files.

In essence, this results in the Application::open signal being emitted in the primary instance.

And so I made a code that supposedly use this functionality. Here is an example code:

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk, Gio

class MyApplication(Gtk.Application):
    def __init__(self):
        super().__init__(
          application_id='org.example.MyApp',
          flags=Gio.ApplicationFlags.HANDLES_OPEN
        )
        self.connect('open', self.do_open)

    def do_open(self, application, files, hint):
        print('do_open')
        for file in files:
            print(f'Opening file: {file.get_path()}')

    def do_activate(self):
        window = Gtk.ApplicationWindow(application=self)
        window.set_title('My Application')
        window.set_default_size(400, 300)
        window.present()

app = MyApplication()
app.run()

But it doesn’t work either. If I call the script and provide a file for it:
./python3 open.py filename.txt

The function “do_open” is never called. No print statements are outputted. Can anyone help me to get this working?

There’s a couple things you have to change here:

  1. Remove the connect
  2. Change the signature of do_open to:
def do_open(self, files, n_files, hint): ...
  1. Pass in the command-line arguments to app.run:
import sys
...
app.run(sys.argv)
2 Likes

It works now, thank you @monster =)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.