Python GTK3:如何创建Gtk.FileChooseDialog? [英] Python GTK3: how to create a Gtk.FileChooseDialog?
问题描述
如何正确创建Gtk.FileChooseDialog?
How to create a Gtk.FileChooseDialog properly?
此热门教程说要使用代码如下:
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
运行此命令(以捕获已弃用的警告),则会显示:
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
使用Gtk.FileChooserDialog.new
给出TypeError: Dialog constructor cannot be used to create instances of a subclass FileChooserDialog
.
API 关于构造函数一无所知.很奇怪.
The API says nothing about the constructor. Weird.
ps:该问题的答案应与python -W error
一起使用.它不应依赖已弃用的API.这就是我要问的全部.
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.
推荐答案
只需遵循说明并使用关键字参数.我也将按钮更改为使用.add_buttons()
,因为这也引发了DeprecationWarning:
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)
这篇关于Python GTK3:如何创建Gtk.FileChooseDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!