python - pyGtk3 对话必须有 parent 吗?如果是这样,您如何从脚本中调用它们?

标签 python pygtk gtk3

我可以有一个调用窗口的脚本,但是当我尝试与 parent = None 进行对话时,我得到:

Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.

我可以将此映射到哪个父级?似乎我可以将它映射到一个虚拟的父级,但这会导致事情破裂和人员死亡吗?


代码来自 http://python-gtk-3-tutorial.readthedocs.io/en/latest/dialogs.html#messagedialog从父窗口调用它的地方......但我希望能够在运行终端脚本时弹出它。

编辑: 一些探索(以及下面的答案提供)发现使用 Gtk.Window() 作为下面的第一个参数(而不是没有)确实摆脱了消息.. .

def on_question():
    dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.QUESTION,
        Gtk.ButtonsType.YES_NO, "This is an QUESTION MessageDialog")
    dialog.format_secondary_text(
        "And this is the secondary text that explains things.")
    response = dialog.run()
    if response == Gtk.ResponseType.YES:
        print("QUESTION dialog closed by clicking YES button")
    elif response == Gtk.ResponseType.NO:
        print("QUESTION dialog closed by clicking NO button")

    dialog.destroy()

最佳答案

我会把上面的评论作为一个答案。 您可能在代码中的某处有一个 w = Gtk.Window() (它可能在函数体内)并将该 w 传递给 on_question:

def on_question(parent=None):
    dialog = Gtk.MessageDialog(parent, 0, Gtk.MessageType.QUESTION,
        Gtk.ButtonsType.YES_NO, "This is an QUESTION MessageDialog")
....
w = Gtk.Window()
on_question(w)

def on_question():
    dialog = Gtk.MessageDialog(Gtk.Window(), 0, Gtk.MessageType.QUESTION,
        Gtk.ButtonsType.YES_NO, "This is an QUESTION MessageDialog")

如果这是唯一的问题的话,Gtk-message 就没了。

关于python - pyGtk3 对话必须有 parent 吗?如果是这样,您如何从脚本中调用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38407707/

相关文章:

python - 谷歌数据流 : global name is not defined - apache beam

gtk - 如何扩展 Gtk 标签以填充整个水平空间?

widget - 如何将独立窗口定位在屏幕上的确切位置?

Python - Gtk.TreeView 和 Gtk.ListStore 获取选定的索引

python - Gtk3 用另一个小部件替换子小部件

python - 为什么我的 gi.repository.Gtk 一直使用 GTK 2 模块?

python - 如何在Python中从串行数据包中提取字节序列并将其更改为正确的整数表示形式?

python - 有没有办法在脚本出现错误后自动运行

python - 有人可以给我关于这个优化策略的两分钱吗

python - Pygtk 线程化长命令行进程