2016-07-03 23 views
2

如何正確創建Gtk.FileChooseDialog?Python GTK3:如何創建一個Gtk.FileChooseDialog?

This popular tutorial說,使用類似下面的代碼:

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)) 

但是,如果你運行這個與python -W error(趕棄用警告),它說:

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 

使用Gtk.FileChooserDialog.newTypeError: Dialog constructor cannot be used to create instances of a subclass FileChooserDialog

The API對構造函數沒有提及。奇怪的。

ps:此問題的答案應與python -W error一起使用。它不應該依賴於已棄用的API。這是我問的所有問題。

回答

3

只需按照說明操作並使用關鍵字參數即可。我也改變了按鈕使用.add_buttons(),因爲那也拋出了一個棄用警告:

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)