3

How to create a Gtk.FileChooseDialog properly?

This popular tutorial says to use code like the following:

import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk dialog = Gtk.FileChooserDialog("Please choose a folder", None, Gtk.FileChooserAction.SELECT_FOLDER, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, "Select", Gtk.ResponseType.OK)) 

But, if you run this with python -W error (to catch the deprecated warnings) it says:

 File "test3.py", line 8, in <module> "Select", Gtk.ResponseType.OK)) File "/usr/lib/python2.7/dist-packages/gi/overrides/__init__.py", line 287, in new_init category, stacklevel=stacklevel) gi.overrides.Gtk.PyGTKDeprecationWarning: Using positional arguments with the GObject constructor has been deprecated. Please specify keyword(s) for "title, parent, action, buttons" or use a class specific constructor. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations 

Using Gtk.FileChooserDialog.new gives TypeError: Dialog constructor cannot be used to create instances of a subclass FileChooserDialog.

The API says nothing about the constructor. Weird.

ps: The answer to this question should work with python -W error. It should not rely on deprecated APIs. It is the all point of me asking.

1 Answer 1

6

Simply follow out the instructions and use keyword arguments. I also changed the buttons to using .add_buttons() since that also threw a DeprecationWarning:

import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk dialog = Gtk.FileChooserDialog( title="Please choose a folder", action=Gtk.FileChooserAction.SELECT_FOLDER, ) dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, "Select", Gtk.ResponseType.OK) 
Sign up to request clarification or add additional context in comments.

3 Comments

Where do you find the documentation please ? I could not find anything
@Mike I haven't worked with Gtk in a few years, but when I did I used the reference here. It's not designed for the Python language, but it's almost all the same structure. I've just now found this that's written for Python that looks very useful, but I've never used it myself. For a general tutorial if you're getting started, I used this.
I've finally found the same as the last one you gave, more precisely here. Thank you !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.