Python GTK3使用GTK.Container创建简单的容器对象



我尝试创建一个具有以下代码的基本GTK容器小部件:

from Gtk3Modules import *
from gi.repository.GObject import GObject
class Ex(Gtk.Container):
    pass

btn = Gtk.Button("nss")
ab = Ex()
ab.add(btn)

w = Gtk.Window()
w.add(ab)
w.show_all()

启动此脚本时,我会收到以下致命错误:

(example.py:2642): Gtk-WARNING **: GtkContainerClass::add not implemented for '__main__+Ex'
**
Gtk:ERROR:gtkwidget.c:12365:gtk_widget_real_realize: assertion failed: (!_gtk_widget_get_has_window (widget))
rlwrap: warning: python3 crashed, killed by SIGABRT (core dumped).
rlwrap itself has not crashed, but for transparency,
it will now kill itself with the same signal

warnings can be silenced by the --no-warnings (-n) option
Aborted (core dumped)

Gtk.Container不是窗口小部件,它是您必须实现的接口。您不太可能实际要做的是实现新容器并不微不足道。

如果您只想要一个孩子,则要使用的是Gtk.Box,如果您希望它包含多个孩子或Gtk.Bin

最新更新