python - 调用 destroy() 后 GTK 窗口不消失
问题描述
我正在尝试使用以下代码显示来自 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 应用程序,只是为了请求确认。
解决方案
你没有给 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)
推荐阅读
- microsoft-graph-teams - 如何在图形 api 中获取微软团队频道连接的文件夹 url?
- ios - 沙盒用户帐户是否与证书和配置文件相关联?
- python - pip install -e,Python 路径和命名空间包
- javascript - Javascript ES6,如果存在则从数组中删除项目的最佳方法,如果不存在则添加
- javascript - 我想每次都在原生反应中重新创建平面列表
- jquery - jQuery,唯一标识
- xml - xml.Unmarshall 多根 XML 在 Go
- grpc - 如何在使用 blazor 的 grpc 调用(或其他异步方法)后使 html 元素显示一个值?
- android - 错误:使用相同的 AVD 运行多个模拟器是 android Studio 中 ubuntu 的实验性功能?
- linq - 从“VisitLambda”调用时,重写“System.Linq.Expressions.ParameterExpression”类型的节点必须返回相同类型的非空值