我正在尝试使用以下代码从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应用程序,只是要求进行确认。
答案 0 :(得分: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)