# Gtk and Cairo Working?

**URL:** <https://discourse.julialang.org/t/gtk-and-cairo-working/23332>\
**Category:** General Usage\
**Created:** [April 20, 2019, 3:45am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332 "2019-04-20T03:45:26Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardAnaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richardanaya/32/8059_2.png) [@RichardAnaya](https://discourse.julialang.org/u/RichardAnaya)\
**Post date:** [April 20, 2019, 3:45am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/1 "2019-04-20T03:45:26Z")

</div>

I’ve been having difficulty getting basic drawing going with Cairo and Gtk. The examples don’t seem to be working due to a type issue with what Gtk returns as a context and what Cairo expects.

```julia
using Gtk, Graphics
c = GtkCanvas()
win = GtkWindow(c, "Canvas")
ctx = getgc(c)
h = height(c)
w = width(c)
rectangle(ctx, 0, 0, w, h/2)
show(c)

```

generates error

```julia
ERROR: LoadError: MethodError: no method matching rectangle(::Gtk.GLib.FieldRef{GtkCanvas}, ::Int64, ::Int64, ::Int32, ::Float64)
Closest candidates are:
  rectangle(!Matched::Cairo.CairoContext, ::Real, ::Real, ::Real, ::Real) at /home/richardanaya/.julia/packages/Cairo/CXPG1/src/Cairo.jl:704
  rectangle(!Matched::GraphicsContext, ::Real, ::Real, ::Real, ::Real) at /home/richardanaya/.julia/packages/Graphics/XgkW4/src/Graphics.jl:452
Stacktrace:
 [1] top-level scope at none:0
 [2] include at ./boot.jl:326 [inlined]
 [3] include_relative(::Module, ::String) at ./loading.jl:1038
 [4] include(::Module, ::String) at ./sysimg.jl:29
 [5] exec_options(::Base.JLOptions) at ./client.jl:267
 [6] _start() at ./client.jl:436

```

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [April 20, 2019, 6:56am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/2 "2019-04-20T06:56:43Z")

</div>

afaics the getgc is already causing a problem as it doesn’t provide a CairoContext. I’d recommend to file an issue

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [April 20, 2019, 7:05am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/3 "2019-04-20T07:05:03Z")

</div>

Actually, it does, but you need to `show(c)` first to fully establish the widget. No need for an issue.

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [April 20, 2019, 9:24am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/4 "2019-04-20T09:24:17Z")

</div>

The example from [http://juliagraphics.github.io/Gtk.jl/latest/manual/canvas.html](http://juliagraphics.github.io/Gtk.jl/latest/manual/canvas.html) works OK for me (Julia 1.1.0):

```julia
using Gtk, Graphics
c = @GtkCanvas()
win = GtkWindow(c, "Canvas")
@guarded draw(c) do widget
    ctx = getgc(c)
    h = height(c)
    w = width(c)
    # Paint red rectangle
    rectangle(ctx, 0, 0, w, h/2)
    set_source_rgb(ctx, 1, 0, 0)
    fill(ctx)
    # Paint blue rectangle
    rectangle(ctx, 0, 3h/4, w, h/4)
    set_source_rgb(ctx, 0, 0, 1)
    fill(ctx)
end
show(c)

```

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [April 20, 2019, 9:53am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/5 "2019-04-20T09:53:59Z")

</div>

Hey, this is definitely a bug within Gtk. The issue is that the cairo context and surface are loaded lazily. There seems to be a reason for this (code is written by @tim.holy or @jameson). However, I have just pushed a fix for it:

[https://github.com/JuliaGraphics/Gtk.jl/commit/15ed88c020e97c750cabad2dccbdf3a8a22b45af](https://github.com/JuliaGraphics/Gtk.jl/commit/15ed88c020e97c750cabad2dccbdf3a8a22b45af)

So  
] add Gtk#master

will solve it until we have made a new release.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [April 20, 2019, 9:55am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/6 "2019-04-20T09:55:07Z")

</div>

It’s not obvious, but the code in `draw(c)` is executed after the `show(c)`

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [April 20, 2019, 10:09am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/7 "2019-04-20T10:09:35Z")

</div>

After `] add Gtk#master` I still get the same error from the original posters code.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [April 20, 2019, 10:18am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/8 "2019-04-20T10:18:27Z")

</div>

yep, something is wrong.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [April 20, 2019, 10:35am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/9 "2019-04-20T10:35:35Z")

</div>

I have pushed something, which at least does not give an error, but it also does not draw. So the original code simply requires the redraw function (which solves the original issue since getgc is only called when the context is available)

---

<div class="post-metadata">

**Author:** ![RichardAnaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richardanaya/32/8059_2.png) [@RichardAnaya](https://discourse.julialang.org/u/RichardAnaya)\
**Post date:** [April 20, 2019, 3:55pm UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/10 "2019-04-20T15:55:15Z")

</div>

Wierd, when I run that code nothing happens at all

---

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [April 20, 2019, 9:00pm UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/11 "2019-04-20T21:00:45Z")

</div>

windows or linux ?  
which version of julia ?

---

<div class="post-metadata">

**Author:** ![RichardAnaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richardanaya/32/8059_2.png) [@RichardAnaya](https://discourse.julialang.org/u/RichardAnaya)\
**Post date:** [April 23, 2019, 8:35am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/12 "2019-04-23T08:35:33Z")

</div>

Just a follow up, everything works now with new version!

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [April 24, 2019, 9:04am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/13 "2019-04-24T09:04:14Z")

</div>

Unfortunately I did not get the original code working (with Gtk#master) :

```julia
julia> ctx = getgc(c)
┌ Error: GtkCanvas not yet initialized.
└ @ Gtk ~/.julia/packages/Gtk/aP55V/src/cairo.jl:123

```

and a bit later

```julia
julia> rectangle(ctx, 0, 0, w, h/2)
ERROR: MethodError: no method matching rectangle(::Gtk.GLib.FieldRef{GtkCanvas}, ::Int64, ::Int64, ::Int32, ::Float64)

```

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [April 24, 2019, 9:57am UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/14 "2019-04-24T09:57:51Z")

</div>

the original code is not supposed to work. Use the form outlined in [http://juliagraphics.github.io/Gtk.jl/latest/manual/canvas.html](http://juliagraphics.github.io/Gtk.jl/latest/manual/canvas.html)  
i.e. the `getgc` should happen in the `draw` callback.

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [April 24, 2019, 1:30pm UTC](https://discourse.julialang.org/t/gtk-and-cairo-working/23332/15 "2019-04-24T13:30:46Z")

</div>

Confirmed, thanks!
