# Example for checkboxes in Gtk.jl

**URL:** <https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028>\
**Category:** General Usage\
**Tags:** question, package, gtk\
**Created:** [July 30, 2022, 2:16pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028 "2022-07-30T14:16:44Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 30, 2022, 2:16pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/1 "2022-07-30T14:16:45Z")

</div>

I am looking for a simple example how to create a dialog with a few checkboxes with Gtk.jl.

Any idea where I could find that?

---

<div class="post-metadata">

**Author:** ![mrs504aa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrs504aa/32/36473_2.png) [@mrs504aa](https://discourse.julialang.org/u/mrs504aa)\
**Post date:** [July 30, 2022, 2:49pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/2 "2022-07-30T14:49:01Z")

</div>

I know there are some examples for Gtk.jl in this repo.  
Not sure if this is what you are looking for.

> **[GitHub - goropikari/GUIAppExample.jl: Examples of Julia GUI Application](https://github.com/goropikari/GUIAppExample.jl)**
>
> Examples of Julia GUI Application. Contribute to goropikari/GUIAppExample.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 30, 2022, 2:57pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/3 "2022-07-30T14:57:54Z")

</div>

Thank you, but only one of the examples uses checkboxes, and it is built with Glade, and I am looking for an example without the use of Glade…

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 30, 2022, 4:26pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/4 "2022-07-30T16:26:05Z")

</div>

Created an issue: [Please add an example how to use a checkbox · Issue #652 · JuliaGraphics/Gtk.jl · GitHub](https://github.com/JuliaGraphics/Gtk.jl/issues/652)

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [July 30, 2022, 6:43pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/5 "2022-07-30T18:43:39Z")

</div>

You might be happy with `GtkObservables`: [GtkObservables.jl/widgets.jl at master · JuliaGizmos/GtkObservables.jl · GitHub](https://github.com/JuliaGizmos/GtkObservables.jl/blob/master/src/widgets.jl#L192)

It should simplify the task of GUI construction for simple GUIs.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 30, 2022, 8:02pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/6 "2022-07-30T20:02:13Z")

</div>

Ok, I have now a checkbox:

```julia
using Gtk.ShortNames, GtkObservables

cb = checkbox()

win = Window("Dialog") |> (bx = Box(:v))
push!(bx, cb)

Gtk.showall(win)
nothing

```

But how can I add a label to it?

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [July 30, 2022, 8:04pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/7 "2022-07-30T20:04:00Z")

</div>

Is this example helpful? [GtkObservables.jl/widgets.jl at master · JuliaGizmos/GtkObservables.jl · GitHub](https://github.com/JuliaGizmos/GtkObservables.jl/blob/master/examples/widgets.jl)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 30, 2022, 8:20pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/8 "2022-07-30T20:20:39Z")

</div>

It helped a little bit:

```julia
using Gtk.ShortNames, GtkObservables

cb1 = checkbox(true, label="Option A")
cb2 = checkbox(true, label="Option B")
btnOK = button(label="OK")

win = Window("Dialog", 200, 72) |> (bx = Box(:v))
push!(bx, cb1)
push!(bx, cb2)
push!(bx, btnOK)

Gtk.showall(win)
nothing

```

Now I would like to have a different window size, but **?Window** tells me " No documentation found"…

**Edit** : By looking at [Getting Started · Gtk.jl](https://juliagraphics.github.io/Gtk.jl/latest/manual/gettingStarted/) I found out how to change the window size, but I still don’t know how to get a list of valid arguments of the constructor **Window**.

And I would like to close the window when I click on the button OK…

Any idea?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 30, 2022, 8:58pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/9 "2022-07-30T20:58:52Z")

</div>

Not working:

```julia
using Gtk.ShortNames, GtkObservables

cb1 = checkbox(true, label="Option A")
cb2 = checkbox(true, label="Option B")
btnOK = button(label="OK")

win = Window("Dialog", 200, 72) |> (bx = Box(:v))
push!(bx, cb1)
push!(bx, cb2)
push!(bx, btnOK)

function on_button_clicked(win)
  println("The button has been clicked")
end
signal_connect(on_button_clicked, btnOK, "clicked")

Gtk.showall(win)
nothing

```

Error message:

```julia
julia> @time include("src/checkbox.jl")
ERROR: LoadError: MethodError: no method matching signal_connect(::typeof(on_button_clicked), ::GtkObservables.Button, ::String)
Closest candidates are:
  signal_connect(::Function, ::GObject, ::Union{AbstractString, Symbol}) at ~/.julia/packages/Gtk/OyCvN/src/GLib/signals.jl:26
  signal_connect(::Function, ::GObject, ::Union{AbstractString, Symbol}, ::Type{RT}, ::Tuple) where RT at ~/.julia/packages/Gtk/OyCvN/src/GLib/signals.jl:4
  signal_connect(::Function, ::GObject, ::Union{AbstractString, Symbol}, ::Type{RT}, ::Tuple, ::Bool) where RT at ~/.julia/packages/Gtk/OyCvN/src/GLib/signals.jl:4
  ...
Stacktrace:
 [1] top-level scope
   @ ~/repos/Dialog/src/checkbox.jl:15
 [2] include
   @ ./client.jl:476 [inlined]
 [3] top-level scope
   @ ./timing.jl:263 [inlined]
 [4] top-level scope
   @ ./REPL[1]:0
in expression starting at /home/ufechner/repos/Dialog/src/checkbox.jl:15

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [July 30, 2022, 9:19pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/10 "2022-07-30T21:19:19Z")

</div>

It has been a long time, but to destroy a window, you call `destroy(win)`, presumably in the callback for the button. There is also `ondestroy`. As for the size, you can set it initially, but Gtk just kinda freezes my Mac, so I can’t play around to find out how to dynamically adjust it.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 30, 2022, 9:22pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/11 "2022-07-30T21:22:32Z")

</div>

Thank you!

One step further:

```julia
using Gtk.ShortNames, GtkObservables

# define the default values
OPTION_A = false
OPTION_B = false

cb1 = checkbox(OPTION_A, label="Option A")
cb2 = checkbox(OPTION_B, label="Option B")
btnOK = button(label="OK")

win = Window("Dialog", 200, 72) |> (bx = Box(:v))
push!(bx, cb1)
push!(bx, cb2)
push!(bx, btnOK)

function on_button_clicked(win)
    global OPTION_A, OPTION_B, win
    destroy(win)
    println(observable(cb1)[], " ", observable(cb2)[])
    OPTION_A = observable(cb1)[]
    OPTION_B = observable(cb2)[]
end
signal_connect(on_button_clicked, widget(btnOK), "clicked")

Gtk.showall(win)
println("Option A: $OPTION_A")
println("Option B: $OPTION_B")
nothing

```

This is partially working (no error message), but I want to have a modal dialog, I want to block the program until I have clicked OK. How can I achieve that?

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [July 30, 2022, 10:15pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/12 "2022-07-30T22:15:07Z")

</div>

I would think something like `setproperty!(win, :modal, true)` should work, but I can’t seem to get it to do so.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 30, 2022, 10:26pm UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/13 "2022-07-30T22:26:39Z")

</div>

If I try your suggestion I get the error:

```julia
ERROR: LoadError: type GtkWindowLeaf has no field modal
Stacktrace:
 [1] setproperty!(x::Gtk.GtkWindowLeaf, f::Symbol, v::Bool)
   @ Base ./Base.jl:39
 [2] top-level scope
   @ ~/repos/Dialog/src/checkbox.jl:24
 [3] include
   @ ./client.jl:476 [inlined]
 [4] top-level scope
   @ ./timing.jl:263 [inlined]
 [5] top-level scope
   @ ./REPL[1]:0
in expression starting at /home/ufechner/repos/Dialog/src/checkbox.jl:24

```

Perhaps that only works with Dialogs and not with Windows? But changing Window into Dialog also fails.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [July 31, 2022, 8:13am UTC](https://discourse.julialang.org/t/example-for-checkboxes-in-gtk-jl/85028/14 "2022-07-31T08:13:07Z")

</div>

The following code works:

```julia
using Gtk.ShortNames, GtkObservables

# define the default values
OPTION_A = false
OPTION_B = false
finished = false 

cb1 = checkbox(OPTION_A, label="Option A")
cb2 = checkbox(OPTION_B, label="Option B")
btnOK = button(label="OK")

win = Window("Dialog", 200, 72) |> (bx = Box(:v))
push!(bx, cb1)
push!(bx, cb2)
push!(bx, btnOK)

function on_button_clicked(win)
    global OPTION_A, OPTION_B, finished, win
    OPTION_A = observable(cb1)[]
    OPTION_B = observable(cb2)[]
    destroy(win)
    finished = true
end
signal_connect(on_button_clicked, widget(btnOK), "clicked")

Gtk.showall(win)
while ! finished
    sleep(0.1)
end

println("Option A: $OPTION_A")
println("Option B: $OPTION_B")
nothing

```

I would be happy if someone comes up with a more simple solution, so I keep the question open for a while…
