编程语言
首页 > 编程语言> > Python GTK3:如何创建Gtk.FileChooseDialog?

Python GTK3:如何创建Gtk.FileChooseDialog?

作者:互联网

如何正确创建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错误运行它(以捕获已弃用的警告),它说:

  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.new给出TypeError:Dialog构造函数不能用于创建子类FileChooserDialog的实例.

The API对构造函数没有任何说明.奇怪的.

ps:这个问题的答案应该与python -W错误一起使用.它不应该依赖于已弃用的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)

标签:python,gtk3
来源: https://codeday.me/bug/20190829/1757396.html