Example for checkboxes in Gtk.jl

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?

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

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…

Created an issue: Please add an example how to use a checkbox · Issue #652 · JuliaGraphics/Gtk.jl · GitHub

You might be happy with GtkObservables: GtkObservables.jl/widgets.jl at master · JuliaGizmos/GtkObservables.jl · GitHub

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

1 Like

Ok, I have now a checkbox:

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?

Is this example helpful? GtkObservables.jl/widgets.jl at master · JuliaGizmos/GtkObservables.jl · GitHub

It helped a little bit:

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 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?

Not working:

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> @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

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.

Thank you!

One step further:

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?

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

If I try your suggestion I get the error:

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.

The following code works:

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…