GUI component interaction: Difference between revisions

Content added Content deleted
(→‎{{header|Julia}}: marked incorrect)
Line 1,884: Line 1,884:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>#=
{{incorrect|Julia|fails with syntax: { } vector syntax is discontinued (line 4) on 1.4}}
The task: For a minimal "application", write a program that
<lang julia>
presents a form with three components to the user: A numeric input
field ("Value") and two buttons ("increment" and "random").
=#

using Tk
using Tk

w = Toplevel("Component Interaction Example")
w = Toplevel("Component Interaction Example")
fr = Frame(w)
fr = Frame(w)
pack(fr, {:expand=>true, :fill => "both"})
pack(fr, expand=true, fill="both")
## The task: For a minimal "application", write a program that
## presents a form with three components to the user: A numeric input
## field ("Value") and two buttons ("increment" and "random").


value = Entry(fr, "")
value = Entry(fr, "")
Line 1,904: Line 1,906:
set_value(value, "0") ## The field is initialized to zero.
set_value(value, "0") ## The field is initialized to zero.


incrementvalue(s) = (val = parse(Int, get_value(value)); set_value(value, string(val + 1)))
tk_bind(increment, "command") do path ## increment its value with the "increment" button.
bind(increment, "command", incrementvalue)
val = get_value(value) | float
set_value(value, string(val + 1))
end



function validate_command(path, P)
function validate_command(path, P)
try
try
if length(P) > 0 parsefloat(P) end
length(P) > 0 && parse(Float64, P)
tcl("expr", "TRUE")
tcl("expr", "TRUE")
catch e
catch e
Line 1,922: Line 1,921:
tcl(W, "delete", "@0", "end")
tcl(W, "delete", "@0", "end")
end
end
tk_configure(value, {:validate=>"key", :validatecommand=>validate_command, :invalidcommand=>invalid_command })


"""
## Pressing the "random" button presents a confirmation dialog, and resets the field's value to a random value if the answer is "Yes".
Pressing the "random" button presents a confirmation dialog and
tk_bind(random, "command") do path
resets the field's value to a random value if the answer is "Yes".
out = Messagebox(w, "Randomize input", "Select a new random number?")
"""
if out == "ok"
function randval(s)
new_value = floor(100*rand(1))[1]
out = Messagebox(w, title="Randomize input", detail="Select a new random number?")
set_value(value, string(new_value))
if out == "ok"
end
new_value = rand(collect(1:99))
set_value(value, string(new_value))
end
end
end
bind(random, "command", randval)

while true sleep(1); end
</lang>
</lang>