Search code examples
pythongtk3introspection

Strange difference. Why different result in FileChooserDialog?


I've battled with the following for a couple of days, and distilled a very compact version of the problem which still shows the issue. The following program shows a basic window, and first opens a FileChooserDialog.

Here's the version which fails - it does not show the Cancel and Accept buttons in the dialog:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk

class Script():
    def __init__(self, parent, width = 800):
        self.parent = parent

    def script_file_dialog(self):
        fc = Gtk.FileChooserDialog(
                    parent = self.parent,
                    title = "title",
                    action = Gtk.FileChooserAction.OPEN,
                    do_overwrite_confirmation = True)
        fc.add_buttons = ("Cancel", Gtk.ResponseType.CANCEL,
                          "Open", Gtk.ResponseType.ACCEPT)

        return fc

class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())
        self.set_default_size(1000, 580)

        self.script = Script(self)

        fc = self.script.script_file_dialog()
        if fc.run() == 1:
            print("one")
        fc.destroy()

        self.show_all()

    def on_test_clicked(self, btn):
        #~ self.script.on_open_script(btn)
        self.script = Script(self)

        fc = self.script.script_file_dialog()
        if fc.run() == 1:
            print("one")
        fc.destroy()

    def run(self):
        Gtk.main()

def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

And the following, almost identical version does work as intended. Note the only difference is when instancing the FileChooserDialog, the buttons are passed as keyword parameters. This is deprecated, and produces a warning.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk

class Script():
    def __init__(self, parent, width = 800):
        self.parent = parent

    def script_file_dialog(self):
        fc = Gtk.FileChooserDialog(
                    parent = self.parent,
                    title = "title",
                    action = Gtk.FileChooserAction.OPEN,
                    do_overwrite_confirmation = True,
                    buttons = ("Cancel", Gtk.ResponseType.CANCEL,
                               "Open", Gtk.ResponseType.ACCEPT))

        return fc

class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())
        self.set_default_size(1000, 580)

        self.script = Script(self)

        fc = self.script.script_file_dialog()
        if fc.run() == 1:
            print("one")
        fc.destroy()

        self.show_all()

    def on_test_clicked(self, btn):
        #~ self.script.on_open_script(btn)
        self.script = Script(self)

        fc = self.script.script_file_dialog()
        if fc.run() == 1:
            print("one")
        fc.destroy()

    def run(self):
        Gtk.main()

def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

I tried delaying the showing of the dialog by triggering it by a button after the main dialog was shown. What's more, I've used the first pattern in other programs, and it's working there.

It's probably the stay-at-home rules which are slowly getting me crazy... Anyone sees the problem?


Solution

  • The first version has a typo:

    fc.add_buttons = ("Cancel", Gtk.ResponseType.CANCEL,
                      "Open", Gtk.ResponseType.ACCEPT)
    

    should be:

    fc.add_buttons("Cancel", Gtk.ResponseType.CANCEL,
                   "Open", Gtk.ResponseType.ACCEPT)