2016-07-03 3 views
2

Gtk.FileChooseDialogを正しく作成する方法は?Python GTK3:Gtk.FileChooseDialogを作成するには?

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でこれを実行した場合、それは言う::

This popular tutorialは、次のようなコードを使用すると言うGtk.FileChooserDialog.newを使用し

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 

TypeError: Dialog constructor cannot be used to create instances of a subclass FileChooserDialogを与えます。

The APIはコンストラクタについて何も言いません。奇妙な。

ps:この質問に対する回答は、python -W errorと一致する必要があります。非難されたAPIに依存するべきではありません。それは私に求めているのです。

答えて

3

指示に従い、キーワード引数を使用します。また、ボタンを.add_buttons()に変更しました。これは、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) 
関連する問題