Bob Liesen <
wb0...@gmail.com> wrote:
> I have a script that talks to a serial port (actually a USB to RS232
> converter) and toggles the RTS line to control a transmitter. It is
> currently hard coded for com5.
> ...
> However, I need a way to pause the script until after a port is
> selected in the list box, otherwise the script runs to the point
> where it tries to open the com port and it crashes because the port
> number variable is empty.
> Clearly, the GUI will need to allow clicking on a listbox member as
> it is paused.
Yes, indeed, it does, but 'paused' is not the right term here.
> Any ideas on how to do this?
There are several ways, but all involve redoing the script to work
differently than how it sounds like you have coded the script so far.
You need to redo the script to generate the GUI, and to wait for a GUI
event (i.e., a click) to occur before it attempts to open the serial
port and continue doing whatever it does after it opens the port.
Which means your "serial port opening and handling" is best put into a
proc, and that proc is not run during the GUI creation phase.
Then, you create the GUI, and setup the widgets such that when one of
them is selected, it calls the proc for serial port handling, passing
the proc the serial port name. Then the proc can go about opening the
port and doing whatever it is that it does after that.
I.e., you need to make the proc that is handling the serial port "event
driven" (as in it reacts to an event happening in the GUI).
Here is a really simple, very minimal example:
---cut---cut---cut---
#!/usr/bin/wish
proc serial-port {port} {
puts stderr "proc serial-port: port='$port'"
# ... do whatever else is normally done
}
button .b1 -text COM1 -command [list serial-port COM1]
button .b2 -text COM2 -command [list serial-port COM2]
button .b3 -text COM3 -command [list serial-port COM3]
pack .b1 .b2 .b3 -side top
---cut---cut---cut---
You'd put the serial port opening inside the "serial-port" proc. When
the script first executes, the proc will be created, but not executed.
Then the script simply creates a stack of button widgets, one per com
port (note, in a real script, I'd create these buttons in a loop, I
simply typed them all out here separately to illustrate). Then the
buttons are packed and the script goes idle, waiting for a button to be
clicked.
Each button has a slightly different proc call attached to it's
-command option. If the mouse cursor clicks on .b2, then .b2's command
that is executed is "serial-port COM2", and if you try this script,
you'll see the puts from the proc showing that the proc was called when
the button was pushed.
There are also several other widgets you can use, menubutton's,
listboxes (as you are already using), you could even use a text widget
or a canvas (although doing so would be excessive just for a "list" of
clickable items). A stack of buttons, a menubutton with an
appropriately configured menu attached, or a listbox with a binding
attached to the <<ListboxSelect>> event would all work. In order of
complexity, a stack of plain buttons is likely simplest, a menu button
is next, and a listbox with a binding to <<ListboxSelect>> is yet a bit
more complicated.