【问题标题】:Drawing in a Gtk.DrawingArea在 Gtk.DrawingArea 中绘图
【发布时间】:2014-05-22 13:05:27
【问题描述】:

我想在我的 Gtk.DrawingArea 对象中绘图。我必须将绘图功能连接到“draw”事件,而不是“expose-event”,因为我正在使用 gtk3。

但这不起作用

这是我的代码:

def draw(widget, context, args=()):

    context.set_source_rgb(0.9, 0, 0.1) #rosso
    context.rectangle(0, 0, widget.get_allocated_width(), widget.get_allocated_height())
    context.fill()


builder = Gtk.Builder()
builder.add_from_file('menuitem.glade')

builder.get_object('drawingarea1').connect("draw", draw)
builder.get_object('drawingarea1').show()

builder.get_object('window1').show() #there are many drawing areas inside a window (they are inside a grid)

Gtk.main()

【问题讨论】:

    标签: gtk draw pygobject drawingarea


    【解决方案1】:

    如果未设置 hexpand 和 vexpand,将 DrawingAreas 添加到 Grid 会有点问题。此外,还需要添加 width_request 和 height_request(或其他一些强制 DrawingArea 具有大小的布局组织),否则初始窗口大小将很小或不可见。下面显示了使用 Grid 和两个 DrawingArea 的代码:

    from gi.repository import Gtk
    
    ui = """
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Generated with glade 3.16.1 -->
    <interface>
      <requires lib="gtk+" version="3.10"/>
      <object class="GtkWindow" id="window1">
        <property name="can_focus">False</property>
        <child>
          <object class="GtkGrid" id="grid1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="column_spacing">4</property>
            <child>
              <object class="GtkDrawingArea" id="drawingarea1">
                <property name="width_request">100</property>
                <property name="height_request">100</property>
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="hexpand">True</property>
                <property name="vexpand">True</property>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">0</property>
                <property name="width">1</property>
                <property name="height">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkDrawingArea" id="drawingarea2">
                <property name="width_request">100</property>
                <property name="height_request">100</property>
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="hexpand">True</property>
                <property name="vexpand">True</property>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">0</property>
                <property name="width">1</property>
                <property name="height">1</property>
              </packing>
            </child>
          </object>
        </child>
      </object>
    </interface>
    """
    
    def draw(widget, context, color=(0, 0, 0)):
        context.set_source_rgb(*color)
        context.rectangle(0, 0, widget.get_allocated_width(), widget.get_allocated_height())
        context.fill()
    
    builder = Gtk.Builder.new_from_string(ui, -1)
    builder.get_object('drawingarea1').connect("draw", draw, (0.9, 0, 0.1))
    builder.get_object('drawingarea2').connect("draw", draw, (0.1, 0, 0.9))
    
    window = builder.get_object('window1')
    window.connect('destroy', Gtk.main_quit)
    window.show_all()
    
    Gtk.main()
    

    【讨论】:

    • 好的,谢谢!好消息是它有效,坏消息是我连接到了错误的空白文件,其中未设置绘图区域(“扩展”属性和宽度)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多