以编程方式关闭 gtk 窗口

发布于 2024-11-06 22:49:36 字数 154 浏览 0 评论 0原文

如果 GTK 中有一个子窗口,并且您想以编程方式关闭它(例如,按保存按钮或退出键),是否有关闭窗口的首选方法?

例如,

window.destroy()
# versus
window.emit('delete-event')

If you have a sub-window in GTK and you want to close it programmatically (e.g., pressing a save button or the escape key), is there a preferred way to close the window?

E.g.,

window.destroy()
# versus
window.emit('delete-event')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南薇 2024-11-13 22:49:36

使用 destroy 方法无法按预期工作,因为不会在被销毁的窗口上调用“删除事件”回调,因此编辑器将没有机会询问用户是否必须保存文件。

[3|zap@zap|~]python
Python 2.7.3 (default, Jul 24 2012, 10:05:38) 
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
>>> w = gtk.Window()
>>> w.show()
>>> def cb(w,e):
...   print "cb", w, e
...   return True
... 
>>> w.connect ('delete-event', cb)
>>> w.destroy()

在上面的示例中,调用 w.destroy() 不会调用回调,而单击“关闭”按钮将调用它(并且窗口不会关闭,因为回调返回 True)。

因此,如果信号处理程序返回 False,则必须发出信号,然后销毁小部件,例如:

if not w.emit("delete-event", gtk.gdk.Event(gtk.gdk.DELETE)):
  w.destroy()

Using destroy method doesn't work as expected, as the 'delete-event' callbacks are not called on the destroyed window, thus a editor, for example, won't have a chance to ask the user if the file has to be saved.

[3|zap@zap|~]python
Python 2.7.3 (default, Jul 24 2012, 10:05:38) 
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
>>> w = gtk.Window()
>>> w.show()
>>> def cb(w,e):
...   print "cb", w, e
...   return True
... 
>>> w.connect ('delete-event', cb)
>>> w.destroy()

In the above example invoking w.destroy() won't invoke the callback, while clicking on the "close" button will invoke it (and window won't close because callback returned True).

Thus, you have to both emit the signal and then destroy the widget, if signal handlers returned False, e.g:

if not w.emit("delete-event", gtk.gdk.Event(gtk.gdk.DELETE)):
  w.destroy()
天生の放荡 2024-11-13 22:49:36

在 PyGTK 中删除窗口(或任何类型的小部件)时,您应该使用 window.destroy() 。当您调用 window.destroy() 时,窗口将自动发出 delete-event 事件。

此外,当使用 PyGTK 为事件发出信号时,几乎总是需要将事件对象传递给发出方法(请参阅 emit 方法的 pyGObject 文档)。当尝试将 gtk.gdk.Event(gtk.EVENT_DELETE) 传递给对象的 delete-event 发出方法时,它将不起作用。例如:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
>>> w = gtk.Window()
>>> w.show()
>>> w.emit("delete-event", gtk.gdk.Event(gtk.gdk.DELETE))
False

也许最好的方法是简单地使用 del 语句,它将自动删除窗口/小部件并为您进行任何必要的清理。这样做比调用 window.destroy() 更“Pythonic”,后者会留下对已销毁窗口的引用。

You should use window.destroy() when deleting a window in PyGTK (or for that matter any kind of widget). When you call window.destroy() the window will emit a delete-event event automatically.

Furthermore, when emitting a signal for an event using PyGTK, it is almost always required to also pass an event object to the emit method (see the pyGObject documentation for the emit method). When an attempt is made to pass a gtk.gdk.Event(gtk.EVENT_DELETE) to an object's emit method for a delete-event it will not work. E.g:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
>>> w = gtk.Window()
>>> w.show()
>>> w.emit("delete-event", gtk.gdk.Event(gtk.gdk.DELETE))
False

Perhaps the best way, though, is to simply use the del statement which will automatically delete the window/widget and do any necessary cleanup for you. Doing this is more 'pythonic' than calling window.destroy() which will leave around a reference to a destroyed window.

终遇你 2024-11-13 22:49:36

适用于 GTK 3.10 及以上版本。

void
gtk_window_close (GtkWindow *window);

请求关闭窗口,类似于
单击窗口管理器关闭按钮。

此函数可与自定义标题栏中的关闭按钮一起使用。

gtk_window_close ()

For GTK 3.10 and above.

void
gtk_window_close (GtkWindow *window);

Requests that the window is closed, similar to what happens when a
window manager close button is clicked.

This function can be used with close buttons in custom titlebars.

gtk_window_close ()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文