# Tk.jl example on OS X

**URL:** https://discourse.julialang.org/t/tk-jl-example-on-os-x/619
**Category:** General Usage
**Created:** [November 28, 2016, 6:15pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619 "2016-11-28T18:15:14Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![PieterjanRobbe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pieterjanrobbe/32/3298_2.png) [@PieterjanRobbe](https://discourse.julialang.org/u/PieterjanRobbe)
#### Post date: [November 28, 2016, 6:15pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/1 "2016-11-28T18:15:14Z")

</div>

I’m trying the [sketch.jl](https://github.com/JuliaGraphics/Tk.jl/blob/master/examples/sketch.jl) example from the [Tk.jl](https://github.com/JuliaGraphics/Tk.jl) package:

On OS X this gives a drawing canvas that is only updated when I switch windows, i.e., force the drawing to be updated. When I add an explicit `Tk.update()` after line 27, I get spurious lines in the drawing:  
 ![](https://global.discourse-cdn.com/julialang/original/3X/1/2/12450a331d903ba083d8f58e08ef48ba9bd8bf32.png)

Anyone knows how to solve this? Is this also a reported issue on linux?

---

<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: [November 28, 2016, 6:45pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/2 "2016-11-28T18:45:49Z")

</div>

With linux i can sketch. When i switch windows (MAP/UNMAP in X11) i get a sigfault…

---

<div class="post-metadata">

### Author: ![PieterjanRobbe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pieterjanrobbe/32/3298_2.png) [@PieterjanRobbe](https://discourse.julialang.org/u/PieterjanRobbe)
#### Post date: [November 29, 2016, 1:21pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/3 "2016-11-29T13:21:20Z")

</div>

[Gtk.jl](https://github.com/JuliaGraphics/Gtk.jl) does the job!

```julia
using Gtk.ShortNames, Graphics

function sketch_window()
    widget = @Canvas()
    window = @Window(widget, "MyCanvas")

    lastx = 0; lasty = 0;

    widget.mouse.button1press = @guarded function (widget, event)
        lastx = event.x; lasty = event.y
    end

    widget.mouse.button1motion = @guarded function (widget, event)
        ctx = getgc(widget)
        move_to(ctx, lastx, lasty)
        line_to(ctx, event.x, event.y)
        stroke(ctx)
        reveal(widget)
        lastx = event.x; lasty = event.y
    end

    show(widget)
end

sketch_window()

```

---

<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: [November 29, 2016, 4:24pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/4 "2016-11-29T16:24:44Z")

</div>

I was about to mention this (use Gtk), but you sounded like, you’re focused on Tk…

---

<div class="post-metadata">

### Author: ![PieterjanRobbe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pieterjanrobbe/32/3298_2.png) [@PieterjanRobbe](https://discourse.julialang.org/u/PieterjanRobbe)
#### Post date: [November 30, 2016, 8:11am UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/5 "2016-11-30T08:11:43Z")

</div>

I was just trying out some of the possibilities for designing a GUI with Julia. Other comments/tricks I should know about are welcome 🙂

---

<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: [November 30, 2016, 5:55pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/6 "2016-11-30T17:55:44Z")

</div>

I’m a gtk user and apart from some BinDeps problems quite satisfied. Recently we had this GUI thread: [Julia for GUI app](https://discourse.julialang.org/t/julia-for-gui-app/416/1) with other ideas for GUI implementations.

---

<div class="post-metadata">

### Author: ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)
#### Post date: [December 1, 2016, 8:40am UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/7 "2016-12-01T08:40:29Z")

</div>

I couldn’t resist taking a stab at this with QML.jl 🙂  
Julia code:  
[https://github.com/barche/QML.jl/blob/master/example/sketch.jl](https://github.com/barche/QML.jl/blob/master/example/sketch.jl)

QML file:  
[https://github.com/barche/QML.jl/blob/master/example/qml/sketch.qml](https://github.com/barche/QML.jl/blob/master/example/qml/sketch.qml)

---

<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: [December 1, 2016, 9:16am UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/8 "2016-12-01T09:16:39Z")

</div>

~20 lines vs. ~40 lines → Gtk is more efficient (SCNR)

---

<div class="post-metadata">

### Author: ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)
#### Post date: [December 1, 2016, 9:57am UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/9 "2016-12-01T09:57:44Z")

</div>

Yes, but on the other hand you get clean separation between GUI (the QML) and data (just the Point here). The QML file, which is the longest, could easily be generated using a GUI designer such as Qt Creator.

---

<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: [December 1, 2016, 12:19pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/10 "2016-12-01T12:19:50Z")

</div>

I’m not seriously (SCNR-\> sorry, could not resist…) comparing the efficiency of different UI toolkits. We see convergence here: Similar code for similar problems.  
btw: about the code and GUI split: GtkBuilder exists and you can do an xml desciption of a GUI or build it with glade and run it or change it dynamically. Convergence again.

---

<div class="post-metadata">

### Author: ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)
#### Post date: [December 1, 2016, 12:24pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/11 "2016-12-01T12:24:27Z")

</div>

Yes, I figured as much (though I had to google SCNR) 🙂  
It’s good to have options for sure, as there is no “one size fits all”.

---

<div class="post-metadata">

### Author: ![PieterjanRobbe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pieterjanrobbe/32/3298_2.png) [@PieterjanRobbe](https://discourse.julialang.org/u/PieterjanRobbe)
#### Post date: [December 1, 2016, 6:48pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/12 "2016-12-01T18:48:50Z")

</div>

`GtkBuilder` looks like a lot of fun though!

@lobingera I’ll go through your example on [https://gist.github.com/lobingera/9722861](https://gist.github.com/lobingera/9722861) one of these days.

---

<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: [December 2, 2016, 11:00am UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/13 "2016-12-02T11:00:51Z")

</div>

I had to replace

```julia
b = Gtk.GtkBuilder(filename=ARGS[1]);

```

with

```julia
b = Gtk.GtkBuilderLeaf(filename=ARGS[1]);

```

in the gist example to get `fwindow2.glade` working.

`gui1.glade` gave an error:

```julia
ERROR: LoadError: gui1.glade:247:1 Invalid object type 'EMC_ToggleAction_ESTOP'
 in Gtk.GLib.GError(::Gtk.##181#184{String,Gtk.GtkBuilderLeaf}) at /home/kjwiik/.julia/v0.5/Gtk/src/GLib/gerror.jl:17
 in #push!#179(::Void, ::String, ::Void, ::Function, ::Gtk.GtkBuilderLeaf) at /home/kjwiik/.julia/v0.5/Gtk/src/builder.jl:20
 in (::Base.#kw##push!)(::Array{Any,1}, ::Base.#push!, ::Gtk.GtkBuilderLeaf) at ./<missing>:0
 in #GtkBuilderLeaf#178(::Void, ::String, ::Void, ::Type{T}) at /home/kjwiik/.julia/v0.5/Gtk/src/builder.jl:4
 in (::Core.#kw#Type)(::Array{Any,1}, ::Type{Gtk.GtkBuilderLeaf}) at ./<missing>:0
 in include_from_node1(::String) at ./loading.jl:488
 in process_options(::Base.JLOptions) at ./client.jl:262
 in _start() at ./client.jl:318

```

Maybe something has changed after the example was written.

---

<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: [December 2, 2016, 12:53pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/14 "2016-12-02T12:53:24Z")

</div>

i think this was (see gist 3 years ago) pre-v0.4 and for sure something is different since then. @tknopp is known to be a glade/GtkBuilder user; Do you have a more recent working example?

---

<div class="post-metadata">

### Author: ![tknopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tknopp/32/3569_2.png) [@tknopp](https://discourse.julialang.org/u/tknopp)
#### Post date: [December 3, 2016, 11:12pm UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/15 "2016-12-03T23:12:46Z")

</div>

it should be

b = Gtk.@GtkBuilder(filename=ARGS[1]);

All Gtk types got this macro constructor since they are actually abstract.

---

<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: [December 4, 2016, 9:29am UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/16 "2016-12-04T09:29:21Z")

</div>

The `gui1.glade` error seems to be coming from a deprecated Gtk2 widget.

---

<div class="post-metadata">

### Author: ![tknopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tknopp/32/3569_2.png) [@tknopp](https://discourse.julialang.org/u/tknopp)
#### Post date: [December 4, 2016, 9:58am UTC](https://discourse.julialang.org/t/tk-jl-example-on-os-x/619/17 "2016-12-04T09:58:47Z")

</div>

Yes indeed one should try to ensure using Gtk3 widgets. Gtk.jl itself has some support for Gtk2 but it is far from complete and in my opinion not advisable to use Gtk2 anymore.
