gpt4 book ai didi

Python GTK3 : how to create a Gtk. 文件选择对话框?

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:51 44 4
gpt4 key购买 nike

如何正确创建一个 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.new 给出 TypeError: Dialog constructor cannot be used to create instances of a subclass FileChooserDialog

The API对构造函数只字不提。很奇怪。

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)

关于Python GTK3 : how to create a Gtk. 文件选择对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38171196/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com