Joseph Quigley <cpu.crazy <at> gmail.com> writes:
> I want to make a text editor. I know that there are hundreds out there,
> but practice makes perfect, and I need the practice. My problem is
> that I have no idea how to make one. I've tried a little already by
> defining line functions:def line1(): l1 =
> raw_input("")
If you intend to make a text editor (something which allows the user to browse
through the text and manipulate it at will) using just raw_input and print, I
think you've set yourself an impossible task. For a console editor you should
probably look at curses (http://www.amk.ca/python/howto/curses/) - which don't
work on Windows, so you'll probably need this:
http://adamv.com/dev/python/curses/ (but according to the site it's
incomplete).
It's probably better to select a GUI toolkit which has an editor widget and
build your editor on that. Tkinter and wxPython both offer this, with wxPython
including a wrapper of the excellent Scintilla control (it has syntax
highlighting, word wrapping, support for non-monospaced fonts, etc.). You could
even write a webbased editor using the text entry widget provided by browsers
(crappy as it may be), even that would be better than hand-coding your own
editor widget.
> line2()Of course, this only allows for 2 lines. What's the trick to an
> infinite number of lines?
lines = []
while True:
lines.append(raw_input(''))
(But that offers no way to e.g. save and continue.)
> YATENJoe"? I'm still a newbie to python (actually OOP programming in
> general!) so I can't have a lot of complicated stuff thrown in my
> face.
Text editors are IMO not applications suitable for learning vanilla Python, or
OOP for that matter. Text editors are used as example applications in the RAD
IDE world (VB, Delphi and such) because in those environments a text editor is
literally three clicks away (click on memo component, click on form, click on
run). Vanilla Python has no GUI, so no memo component - hence my recommendation
for Tkinter or wxPython. PythonCard and Boa provide an experience somewhat
similar to commercial RAD IDE's if that's what you're looking for.
> I thought of a function that would define a function for me. Is this
> possible? If it is how would I do it? If it possible, here's my
Having one function for each line is a bad idea whether you code it by hand or
automatically (yes, it would be possible, but not something you should want to
do). A line of text is a piece of *data* similar to the line before it and the
line after it.
A function implements certain behavior. Having the same behavior (raw_input)
implemented separately for each line is an extremely wasteful way of
programming. Use data containers (like lists) for storing data and use a single
function for manipulating any item (line) in such a data structure. E.g. let's
say you have 5 lines of text and you want to uppercase them all. Your way would
be to create 5 separate functions (or 10000000 if there are more lines) for each
of the lines and call each of those. A better way would be to have a single
function and call that repeatedly telling it on which line to operate.
I would suggest that you learn a bit more about Python's facilities before
embarking on an ambitious project :) - you won't get very far without knowing
how to use lists and dictionaries.
Yours,
Andrei
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor