【问题标题】:GTK window does not disappear after calling destroy()调用 destroy() 后 GTK 窗口不消失
【发布时间】:2018-11-26 20:51:56
【问题描述】:

我正在尝试使用以下代码显示来自 Python 3 脚本的确认窗口:

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

dialog = Gtk.MessageDialog(modal=True, buttons=Gtk.ButtonsType.OK_CANCEL)
dialog.props.text = "Why won't this window dissappear?"
response = dialog.run()
dialog.destroy()
dialog.destroy()
dialog.destroy()

if response == Gtk.ResponseType.OK:
    print('OK')
else:
    print('Cancel')

time.sleep(100000)

我希望单击“确定”或“取消”后窗口会消失。但是,在程序结束之前,该窗口仍然可见。我该怎么做才能使窗口消失?

注意:我想在一个简单而线性的 shell 脚本中提示用户进行确认。我不打算实现一个完整的 GTK 应用程序,只是为了请求确认。

【问题讨论】:

    标签: python gtk


    【解决方案1】:

    你没有给 Gtk 时间(或命令)来更新被破坏的窗口。试试这个代码:

    import time
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    
    dialog = Gtk.MessageDialog(modal=True, buttons=Gtk.ButtonsType.OK_CANCEL)
    dialog.props.text = "Why won't this window dissappear?"
    response = dialog.run()
    dialog.destroy()
    while Gtk.events_pending():
        Gtk.main_iteration()
    
    if response == Gtk.ResponseType.OK:
        print('OK')
    else:
        print('Cancel')
    
    time.sleep(1000000)
    

    【讨论】:

      猜你喜欢
      • 2023-02-17
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2011-11-26
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多