如何将 X11 本机(或 SDL)窗口指定为 GTK 对话框的父窗口?

LGB*_*árt 5 x11 gtk sdl gtk3 sdl-2

我有一个仅将 GTK+ 3 用于某些任务的项目,例如提供带有gtk_file_chooser_dialog_new. 我必须设置parent为,NULL因为我没有“主”GTK 窗口。然而,很烦人的是我的程序的标准输出被这样的消息淹没(特别是因为我使用标准输入和 libreadline 来获得命令行界面):

Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
Run Code Online (Sandbox Code Playgroud)

我怎样才能避免这种情况?或者更好(可能也是更正确的方法),而不仅仅是避免:我可以以某种方式将 X11 窗口指定为 GTK 小部件的父窗口,而无需做太多工作吗?实际上,这是一个 SDL 项目,但我不想为此将 SDL“嵌入”到 GTK 窗口中。但是我可以用SDL_GetWindowWMInfo(). 在我看来,这比仅仅“避免”来自 GTK 的消息要好(但我什至不知道该怎么做),因为 - 例如 - 窗口管理器知道对话框和主(SDL/ X11) 窗口等...

这只是一个旁注(实际上应该是另一个问题),似乎在 GTK 文件选择期间完成的各种 GUI 事件(鼠标单击、按键等)可能会被 SDL“感知”然后(在对话框关闭后等) ) 这当然是不想要的。我想我应该尝试从不需要的事件中清空 SDL 事件队列......

jco*_*ens 0

这不是一个完全优雅的解决方案,但我想你可以制作一个(空的)主窗口,并删除“装饰”,使屏幕上基本上不可见。

您没有指定您正在使用哪种语言,这使得我们很难提供帮助。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  test_invisible_main.py
#
#  Copyright 2022 John Coppens <john@jcoppens.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#


import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GooCanvas', '2.0')
from gi.repository import Gtk, GooCanvas

class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())
        self.set_decorated(False)
        self.set_size_request(1, 1)

        dlg = Gtk.FileChooserDialog(parent = self,
                    action = Gtk.FileChooserAction.OPEN)
        if len(args) == 2:
            dlg.set_current_folder(arg[1])
        dlg.add_buttons('Cancel', Gtk.ResponseType.CANCEL,
                        'Ok', Gtk.ResponseType.ACCEPT)
        if dlg.run() == Gtk.ResponseType.ACCEPT:
            with open('filename', 'w') as outfile:
                outfile.write(dlg.get_filename())
        dlg.destroy()

        self.show_all()
        exit(1)
        Gtk.main_quit()

    def run(self):
        Gtk.main()


def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))
Run Code Online (Sandbox Code Playgroud)

该程序创建一个大小为 1 x 1 的主窗口 - 基本上不可见。它启动文件选择器,等待选择,并将文件名写入 file 。启动程序时,单个参数可以设置 FileChooser 的初始文件夹。

如果需要,可以exit(1)将 设定为 0/1,以便在OkCancel被按下时进行通信。