Python Widget to read in user input box in blog

Discussion in 'Python' started by ecpbm765, Apr 24, 2007.

  1. ecpbm765

    ecpbm765 Guest


    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?

    ecpbm765, Apr 24, 2007
    1. Advertisements

  2. ecpbm765

    James Stroud Guest

    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))

    if __name__ == "__main__":

    Etc. includes copy and paste events with mouse.

    James Stroud, Apr 24, 2007
    1. Advertisements

  3. ecpbm765

    half.italian Guest

    Here's another way. Although I'm not really sure whether you are
    talking about a web app or a local gui app.

    #! /usr/bin/env python

    from Tkinter import *

    class Window(Frame):
    def __init__(self, parent=None):
    Frame.__init__(self, parent)
    Label(self, text="Enter the path to a gif").pack(padx=5, pady=5)
    self.label = Label(self, text="")
    self.label.pack(padx=5, pady=5)
    self.entry = Entry(self, text="")
    self.entry.pack(padx=5, pady=5)


    def update(self):
    self.image = PhotoImage(file=self.entry.get())
    except TclError:
    self.label.config(text=self.entry.get(), image="")
    self.after(20, self.update)

    if __name__ == '__main__':
    root = Tk()

    half.italian, Apr 24, 2007
  4. Are you talking about a webapplication here? If yes - and the mentioning of
    HTML somehow implies that - you can't do that in python, as client-side
    code in browsers is Javascript.

    However there are frameworks - TurboGears, Django, Pylons - that make rapid
    web-application development easy, with good ajax-support.

    Diez B. Roggisch, Apr 24, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.