There are a couple of problems with this code:
1. If you add a line to the window, you must move the mouse into
the window before the window gets redrawn. Shouldn't "show"
have caused this to happen? Or is there some sort of "really
render" routine to cause this. Note: to duplicate the problem,
run l=test(), then do: l.writeline('hello'). I suppose I need
to make a stdwin.change call, right?
2. I thought it would be cool if there is a window handler thread for
each window. That way, the windows can be scrolled while the user
is typing stuff in the main python window (or while your code is
looping). Unfortunately, getevent doesn't let you say
"getevent(win)" so you get everyone's events. If you try pushing
them back, you'll just grab them again. Is there any way to
do this cleanly. I know the way I do it now (using mainloop)
is wrong since it grabs events for everyone; it also crashes with two
windows (they'll come up, but after a few scrolls, you lose). And
you run into big problems if you try to bring wdb up.
This really isn't critical stuff. Just playing. Any advice is appreciated.
# logwin.py -- an output logging window (e.g., for error msgs, tracing, etc.)
#
# Example:
#
# log=LogWindow('my errors') # creates a log window titled 'my errors'
# log.writeline('hello world') # output a line to the window
import stdwin
import srcwin
import thread
import sys
sys.check_interval=1000 # set to reasonable value for thread
import mainloop
class LogWindow(srcwin.TextWindow):
def __init__(self, title):
# To avoid re-writing code, call __init__
# with 10 lines to set size of window when pops up
srcwin.TextWindow.__init__(self, title, '\n'*10)
self.contents='' # now zero out the contents
self.linecount=0 # and also the number of lines
shift=2*stdwin.textwidth('0') # shift over text further right
self.editor.move((self.leftmargin+shift,self.top), \
(self.rightmargin,self.bottom))
# now create a handler for my window as a thread
thread.start_new_thread(self.handle_windows,())
# handle events for this window as a thread
def handle_windows(self):
mainloop.mainloop() # ulp! handle everyone!
thread.exit_thread()
# use write to append a line to the output window so it works like
# any other stream
def write(self, line):
self.contents=self.contents + line
self.linecount=self.linecount + 1
self.editor.settext(self.contents) # really reset the contents
self.bottom = self.bottom + self.lineheight
self.win.setdocsize(0,self.bottom) # for scroll bars to work
self.showline(self.linecount) # show last line
def writeline(self, line):
self.write(line + '\n')
# ------------------------------ testing ------------------------------
def test():
log = LogWindow('my log')
for i in range(1,20):
log.writeline('this is line'+`i`)
return(log) # so user can write to it too