widget - 使用 GTK3 编写自定义小部件

标签 widget gtk custom-controls pygtk

我正在尝试找到为 Gtk-3 编写的自定义小部件的最简单示例。

到目前为止,我发现的最好的东西是 this (使用 PyGTK),但它似乎是针对 Gtk-2 的。

顺便说一句:我不在乎它是用什么语言编写的,但如果我们可以避免使用 C++,那就更好了!

最佳答案

它是Python3 Gtk3,那么:

from gi.repository import Gtk

class SuperSimpleWidget(Gtk.Label):
    __gtype_name__ = 'SuperSimpleWidget'

这是一个不平凡的示例,它实际上做了一些事情,即绘制其背景并通过它绘制一条对角线。我从 Gtk.Misc 而不是 Gtk.Widget 继承来保存一些样板文件(见下文):

class SimpleWidget(Gtk.Misc):
    __gtype_name__ = 'SimpleWidget'

    def __init__(self, *args, **kwds):
        super().__init__(*args, **kwds)
        self.set_size_request(40, 40)

    def do_draw(self, cr):
        # paint background
        bg_color = self.get_style_context().get_background_color(Gtk.StateFlags.NORMAL)
        cr.set_source_rgba(*list(bg_color))
        cr.paint()
        # draw a diagonal line
        allocation = self.get_allocation()
        fg_color = self.get_style_context().get_color(Gtk.StateFlags.NORMAL)
        cr.set_source_rgba(*list(fg_color));
        cr.set_line_width(2)
        cr.move_to(0, 0)   # top left of the widget
        cr.line_to(allocation.width, allocation.height)
        cr.stroke()

最后,如果您确实想从 Gtk.Widget 派生,那么您还必须设置绘图背景。 Gtk.Misc 会为您完成此操作,但 Gtk.Widget 可能是一个本身并不实际绘制任何内容的容器。但好奇的人想知道,所以你可以这样做:

from gi.repository import Gdk

class ManualWidget(Gtk.Widget):
    __gtype_name__ = 'ManualWidget'

    def __init__(self, *args, **kwds):
        # same as above

    def do_draw(self, cr):
        # same as above

    def do_realize(self):
        allocation = self.get_allocation()
        attr = Gdk.WindowAttr()
        attr.window_type = Gdk.WindowType.CHILD
        attr.x = allocation.x
        attr.y = allocation.y
        attr.width = allocation.width
        attr.height = allocation.height
        attr.visual = self.get_visual()
        attr.event_mask = self.get_events() | Gdk.EventMask.EXPOSURE_MASK
        WAT = Gdk.WindowAttributesType
        mask = WAT.X | WAT.Y | WAT.VISUAL
        window = Gdk.Window(self.get_parent_window(), attr, mask);
        self.set_window(window)
        self.register_window(window)
        self.set_realized(True)
        window.set_background_pattern(None)

编辑并实际使用它:

w = Gtk.Window()
w.add(SimpleWidget())
w.show_all()
w.present()
import signal    # enable Ctrl-C since there is no menu to quit
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()

或者,更有趣的是,直接从 ipython3 REPL 使用它:

from IPython.lib.inputhook import enable_gtk3
enable_gtk3()
w = Gtk.Window()
w.add(SimpleWidget())
w.show_all()
w.present()

关于widget - 使用 GTK3 编写自定义小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18160315/

相关文章:

javascript - 如何制作像 Disqus 或 IntenseDebate 这样的 javascript

opengl - Pygobject GTK3 中 Gtk.GLArea 的使用

c - 将结构中的结构传递给 GSourceFunc 的最佳方法

c++ - 如何 "disconnect"加速器、按键事件或鼠标按下事件在 GTK+ 小部件中?

html - 修改预装 WordPress 小部件的外观

Android - 获取计时器小部件的时间

upload - Youtube UploadWidget上的onUploadStart吗?

c - 如何使用 GTK/Cairo 将多个 PNG 合成为单个 PNG

c# - Visual Studio 2008 IDE 无法正确呈现自定义控件

delphi - 自定义 TControl 在剪切和粘贴之前不会在设计时绘制