Events on Julia

,

Julia has something like Events? “on” → “event” → callback
thanks!

No and maybe. The idea of event callbacks is inherently tied to GUI programming, and Julia has no built-in GUI functionality.

There are GUI packages, but I can’t answer to how any of them deal with callbacks. I would pick one first, and then figure out how it does this.

Observables.jl has observables with listeners and callbacks https://github.com/JuliaGizmos/Observables.jl/

1 Like

As @gustaphe said, yes, this idea is not inherent to Julia’s base.
However, the tool you’d probably want to look at (aside from @jules 's suggestion) is GTK.jl if you are interested in GUI programming.

Example from docs here:

using Gtk

win = GtkWindow("My First Gtk.jl Program", 400, 200)

b = GtkButton("Click Me")
push!(win,b)

function on_button_clicked(w)
  println("The button has been clicked")
end
signal_connect(on_button_clicked, b, "clicked")

showall(win)
1 Like

I tried something with Reactive.jl, copy of its documentation.

using Reactive, Random, Printf

sigVotes = Signal(:NoVotes)
candidates = [ :Alice, :Queen, :NoVotes ]

aVotes = filter(v -> v == :Alice, sigVotes)
qVotes = filter(v -> v == :Queen, sigVotes)
nVotes = filter(v -> v == :NoVotes, sigVotes)

function count(cnt, _)
    cnt+1
end

aCount = foldp(count, 0, aVotes)
qCount = foldp(count, 0, qVotes)
nCount = foldp(count, 0, nVotes)

leading = map(aCount, qCount, nCount) do a, q, n
    if a == 0 || q == 0
        :NoVotes
    else
        if a > q
          :Alice
        elseif q > a
          :Queen
        else
          :Tie
        end
    end
end

function voting()
    for i in 1:100
        # change here, trigger to all
        push!(sigVotes, rand(candidates))
    end
    println(leading)
    println(@sprintf("Alice: %d x %d Queen (NoVotes: %d)", value(aCount), value(qCount), value(nCount)))
end

voting()

# vote again!
# voting()