A window

ztk is the Tk GUI toolkit on ztcl. A window is a tree of widgets with names such as .f.list, and a widget is a command. Geometry managers (grid and pack) place the widgets, and bind runs a script on an event. This lesson gives jot a window.

The jot window

The window below runs in this page with ztk on WebAssembly. Add a note, and double-click a note to mark it done. Its code is below the window. On your computer, save the code as jot-window.tcl and run ztk jot-window.tcl.

ToyThe jot windowA ztk window with the notes of jot. Type a note and press Enter, or double-click a note.

It starts when it comes into view. It needs JavaScript and WebAssembly.

How this window is made: 46 lines of Tcl with the ztk GUI toolkit
# height: 300
# shell: none
# toy
set notes [list [dict create text "buy milk" done 1] [dict create text "fix the bike" done 0]]
set text ""
ttk::frame .f -padding 10
pack .f -fill both -expand 1
ttk::entry .f.e -textvariable text
ttk::button .f.add -text "Add" -command add
listbox .f.list -height 7 -activestyle none
ttk::label .f.status -textvariable status
grid .f.e .f.add -sticky ew -pady {0 8}
grid .f.list - -sticky nsew
grid .f.status - -sticky w -pady {6 0}
grid columnconfigure .f 0 -weight 1
grid rowconfigure .f 1 -weight 1
bind .f.e <Return> add
bind .f.list <Double-1> toggle

proc refresh {} {
    global notes status
    .f.list delete 0 end
    set open 0
    foreach n $notes {
        set done [dict get $n done]
        .f.list insert end "[expr {$done ? "✓" : "•"}] [dict get $n text]"
        if {!$done} { incr open }
    }
    set status "$open open of [llength $notes]"
}
proc add {} {
    global notes text
    if {[string trim $text] eq ""} return
    lappend notes [dict create text $text done 0]
    set text ""
    refresh
}
proc toggle {} {
    global notes
    set i [.f.list curselection]
    if {$i eq ""} return
    set n [lindex $notes $i]
    lset notes $i [dict replace $n done [expr {![dict get $n done]}]]
    refresh
}
refresh

How it works

Reading code an agent wrote

Ask an agent

Add a "Remove done" button to the jot window that removes each note that is done. Keep the notes in the variable notes, and call refresh after each change.

Try this