Skip to main content
 首页 » 编程设计

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

2024年10月17日8JeffreyZhao

我可以有一个调用窗口的脚本,但是当我尝试与 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 就没了。