banging head against sys.stdin and tkinter on win32

Chris Tavares christophertavares at earthlink.net
Tue Dec 11 03:26:20 EST 2001


"David Mallwitz" <dmallwitz at cox.rr.com> wrote in message
news:3c15a9e3$1_2 at goliath.newsgroups.com...
> thump thump thump
>
>     A few weeks ago I decided to try to become more proficient in Python
by
> writing a plain vanilla GUI app that interfaces with a serial port. Turns
> out to have been pretty straightforward and everything is now at least
> working, if not working the way I want it to. The program is a single
> Tkinter text widget in its own thread to display everything that is read
> from the serial port, another thread to read from that port, and one more
> thread to continuously read input from the user. I don't need to process
any
> input from the user, just send it straight to the device attached to the
> serial port.
>     So far no problem. I've been using a while 1 loop and msvcrt to poll
for
> any keystrokes, but this keeps a console window open and all commands must
> be typed in that window. I'd like to 86 the console and just have all
> input/output done on the display window (I'm familiar with the .pyw
filename
> trick, won't work, msvcrt need that console window to function). This is
> where the headbanging thing happens.
>     I've tried wrapping the window in a class and defining a read method
in
> order to redirect sys.stdin - no joy, msvcrt just wants to use a console
> window no matter what. I used an Entry widget for a while, but the number
of
> bind statements was prohibitively long, and would all need to be changed
if
> I tried to communicate with a different type of device. Input and
raw_input
> don't really apply here since I need to work on a character by character
> basis and not wait for \n to terminate a string.
>     Am I even on the right track here? I'll gladly RTFM if someone will
> point me to where an appropriate FM is. For that matter, where is the
source
> code for msvcrt? There is no file with that name in my Python path (or
> anywhere else), yet I have to import it as a module.
>
> Best,
> Dave
>

You're not on the right track. Now that that's taken care of :-)...

msvcrt is an extension module written in C (actually, it just provides
Python interfaces to a bunch of functions in the MS C Runtime library), so
that's why there's no source code. It's in the Python source distribution if
you're really interested. The function in question is actually a Win32
CONSOLE function, and so it needs a console to work. Thus, redirecting stdin
won't help.

The right way to handle this problem is what you were doing - use an entry
widget (or whatever they call it in TK) - trap the events as the buffer
changes and send out the keystrokes to the serial port. Unfortunately I
haven't touched Python GUI programming in a LONG time, so I can't help you
with the bindings, sorry.

-Chris






More information about the Python-list mailing list