0

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?

1 Answer 1

1

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) 
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Cheers, Damian. You can't imagine the hours I've been staring at this!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.