Python GTK3:如何创建Gtk.FileChooseDialog?

2024-04-13

如何正确创建 Gtk.FileChooseDialog?

这个流行的教程 http://python-gtk-3-tutorial.readthedocs.io/en/latest/dialogs.html#id2说使用如下代码:

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

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

The API https://lazka.github.io/pgi-docs/#Gtk-3.0/classes/FileChooserDialog.html没有提及构造函数。诡异的。

ps:这个问题的答案应该与python -W error。它不应依赖已弃用的 API。这是我问的重点。


只需按照说明操作并使用关键字参数即可。我还将按钮更改为使用.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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python GTK3:如何创建Gtk.FileChooseDialog? 的相关文章

随机推荐