Version 1 of A little fortune teller

Updated 2003-07-24 08:55:32

if 0 {Richard Suchenwirth 2003-07-24 - Linux systems, as well as Cygwin, contain the fortune(6) command which gives you a random adage every time you call it. It draws it from a set of files found in /usr/share/fortune. The big files are plain text, sections delimited by a % sign. Here is a simple fortune viewer that runs on Windows too - you just have to obtain a fortune file from somewhere, and put its path into the source. The viewer uses a message widget, which I've almost never done before, but it has the advantage that it resizes automatically. Click on it for another fortune. Enjoy!

http://mini.net/files/tkfortune.jpg }

  package require Tk
  pack [message .m -font Courier -textvariable fortune -width 800]
  bind . <1> showFortune

  set filename /usr/share/fortune/fortunes ;# put your own here

  set size [file size $filename]
  set fp [open $filename]

 proc showFortune {} {
     global fp size fortune
     seek $fp [expr {int(rand()*$size)}]
     while {[gets $fp] != "%"} {}
     set fortune ""
     while {[gets $fp line]>=0 && $line!="%"} {
         append fortune $line\n
     }
  }
  showFortune

Arts and crafts of Tcl-Tk programming