Expect and Tk: do they play well together?

Posted by de_clarke on 2014-01-11 11:05

I've been playing about (for fun) with a Tk GUI for a cmd line app, trying to drive the app with Expect.

Initial results are a bit confusing; Expect cmds which work in a Tcl interp (no Tk) seem not to work when embedded in procs called by button-press bindings in Tk. Before I spend more time trying to figure out what's going on, thought I'd ask whether this is a known problem with a known workaround (or without a known workaround!)... googling around I found some tantalising hints in old Tcl discussions (like 10 years old) about event-loop conflicts between Tk and Expect, but there the fossil record seems to end.

Is there any well-known problem using the Expect package in a Tk app?

de_clarke | Sat, 2014-01-11 21:11

I'm a little puzzled by the behaviour, but I think I have the workaround.

Subtle things don't work right when Expect commands are executed at other than level #0 -- even with a plain Tcl interp and no Tk. For example:

if I execute the original "spawn" in a proc, I appear to get a connection, but after that "send" and "expect" don't work as one would, um, expect. there's some mashing of stuff echoed to stdout (send_user) and the script hangs. if I change the spawn cmd to "uplevel #0 spawn..." then suddenly I get correct behaviour.

more subtly (and this is rather interesting), I'm driving VLC in rc mode and if I execute "send playlist" in a proc, the playlist I get back shows the *filename* of the mp3 file being played. if I "uplevel #0" that same send command, the playlist shows the id3tag title of the track instead of the filename. I find this truly peculiar; it seems like there's some kind of invisible access to global vars going on. whatever the cause, the cure is now obvious -- I'm going to ensure that all Expect commands execute at level 0.

I no longer believe that these problems are Tk-related. When I use the updated procs with my skeletal Tk GUI, they work. Whatever the issue is, it has to do with the exec level at which Expect commands are issued, not with the Tk event loop.