Skip to content

JuliaInterop/TclTk.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

459 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo Julia interface to Tcl/Tk

Docs License Build Status Coverage Aqua QA

This package provides an optimized Julia interface to Tcl/Tk. The documentation is available on-line and extended examples are given in the examples directory. To whet your appetite, a few examples are given below.

Tcl scripts and commands

The traditional "Hello world!" example:

julia> using TclTk

julia> tcl_eval("puts {Hello world!}")
Hello world!

which shows how to evaluate a Tcl script. The above output is not the value returned by the script as tcl_eval returns nothing by default. If we are interested by the result of the evaluated script or expression, tcl_eval takes an optional first argument to specify the type of the expected result. For example:

julia> tcl_eval(Float64, "expr {4*atan(1)}")
3.141592653589793

The most general return value is a Tcl object which can be reused or converted later:

julia> x = tcl_eval(TclObj, "format \"%s/data-%06d.bin\" \"/tmp\" 123")
TclObj("/tmp/data-000123.bin")

julia> String(x) # `string(x)` and `convert(String, x)` work as well
"/tmp/data-000123.bin"

Spaces and braces are special characters in Tcl and may have to be properly escaped in scripts, perhaps with the help of TclTk.escape_string. A better solution, if the script is a single Tcl command, is to call tcl_exec which assumes that each argument (after an optional leading type) is a single Tcl token:

julia> msg = "- - } Hello world! { - -"
"- - } Hello world! { - -"

julia> x = tcl_exec(:puts, msg)
- - } Hello world! { - -

julia> x = tcl_exec(String, "format", "%s/%s/data-%06d.bin", ENV["HOME"], "tmp", 123)
"/home/eric/tmp/data-000123.bin"

Widgets and images

Below is a simple example to show an image in a Tk top-level window:

julia> using TclTk, TestImages

julia> img = testimage("mandrill"); # read some image data

julia> top = Toplevel(background="#282c34")
Toplevel(".top1")

julia> top.title("A Nice Image")

julia> lab = Label(top, image=TkPhoto(permutedims(img)), cursor="target")
Label(".top1.lab1")

julia> lab.pack(side="top", padx=20, pady=30)

The different stages are:

  • Load some image data as a Julia array.

  • Create a top-level window top with a given background color.

  • Call window manager wm.title command to set the title of the top-level window.

  • Create a widget, here a label lab, whose parent is top to display the image.

  • Call the pack geometry manager to specify how to display the lab widget in its parent (leading Nothing argument is because we are not interested in the result of this command).

It may be noticed that, following Tk conventions, the width and height of an image are its respective first and second dimensions. The images provided by the TestImages have a different convention and we call permutedims(img) to cope with that. Using the apostrophe, i.e. img', would also do the job.

A Tk widget instance can be called to perform widget actions such as (re-)configuring some options. For example, let us show the frame of the image with a sunken relief:

julia> lab.configure(borderwidth=3, relief=:sunken, padding=7, background="beige")

The change should be immediate as Tk widgets apply their configuration dynamically.

Alternatives

There exists another Julia Tk package but with different design choices and some issues I wanted to avoid (for instance, X conflict with PyPlot when using Gtk backend, Qt backend is OK). This is why I started this project. I would be very happy if, eventually, the two projects merge.

Installation

It is easy to install TclTk from the REPL of Julia's package manager[pkg] as follows:

pkg> add TclTk

To check whether TclTk package works correctly:

pkg> test TclTk

To update to the last version:

pkg> update TclTk

and perhaps test again...

If something goes wrong, it may be because you already have an old version of TclTk. Uninstall TclTk as follows:

pkg> rm TclTk
pkg> gc
pkg> add TclTk

before re-installing.


  • [pkg] To switch from julia REPL to the package manager REPL, just hit the ] key and you should get a ... pkg> prompt. To revert to Julia's REPL, hit the Backspace key at the ... pkg> prompt.

About

Optimized Julia interface to Tcl/Tk

Resources

License

Stars

6 stars

Watchers

1 watching

Forks

Contributors

Languages