Python Widget to read in user input box in blog

James Stroud jstroud at mbi.ucla.edu
Tue Apr 24 06:00:21 CEST 2007


ecpbm765 at gmail.com wrote:
> Hey,
> 
> I am helping to develop a project that displays images based on user
> input.  One possible way of implementing this is via a widget that
> when it is run, would read in the users input from an input text field
> (probably from a blog), and replace it with the HTML that would
> display those images.  This is more a proof of concept, so really all
> I am wondering is if there is a good way in Python to read in the text
> the user has typed and change it before the user hits submit?
> 
> Thanks
> 

You can bind KeyRelease, etc. like such:


#! /usr/bin/env python

from Tkinter import *
from ScrolledText import ScrolledText

def format(t):
   return t.replace('.','!')

def dobind(tin, tout):
   def doit(e=None):
     tout['state'] = NORMAL
     tout.delete('1.0', END)
     newtext = format(tin.get('1.0', END).strip())
     tout.insert(END, newtext)
     tout['state'] = DISABLED
   return doit

def main():
   tk = Tk()
   textin = ScrolledText(tk)
   textin.pack(expand=NO, fill=X)
   textout = ScrolledText(tk)
   textout['state'] = DISABLED
   textout.pack(expand=NO, fill=BOTH)
   textin.bind('<KeyRelease>', dobind(textin, textout))
   tk.mainloop()

if __name__ == "__main__":
   main()


Etc. includes copy and paste events with mouse.

James



More information about the Python-list mailing list