[Tutor] Text Editor focus() help

Corey Woodworth cdwom@mpinet.net
Sat, 31 Mar 2001 15:52:35 -0500


I'm trying to get my text editor to open a document in a new toplevel widget
if the current widget contains text. It works fine but the focus sometimes
reverts back to the previous window (and then sometimes it doesn't). How can
I make sure that the new window always receives the focus? Here is my code:
(its 142 lines long. I hope this doesn't offend anyone... I don't think it
will after reading the posting scripts thread, but it still seems a bit
edgy. If This is too long please tell me, and if you notice any other
errors, please point them out to me too :) Thanks

Corey

from Tkinter import *
from ScrolledText import *
import tkMessageBox
from tkFileDialog import *
import MDI
import fileinput
import string

editorList = []
root = None

def die():
    sys.exit(0)

def about():
    tkMessageBox.showinfo("PythonPadTk", "PythonPadTk v0.0\n"
                      "Based on Tkeditor 1.0 from:\n"
                      "For Teach Yourself Python in 24 Hours")

class statusBar(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)
        self.label.pack(fill=X)

    def set(self, format, *args):
        self.label.config(text=format % args)
        self.label.update_idletasks()

    def clear(self):
        self.label.config(text="")
        self.label.update_idletasks()

class editor:
    def __init__(self, rt):
        if rt == None:
            self.window = Tk()
        else:
            self.window = Toplevel(rt)

        self.window.title("PythonPadTk - Untitled")
        self.bar = Menu(self.window)
        self.fileName = None

        self.filem = Menu(self.bar)
        self.filem.add_command(label="New window", underline=0,
command=newEditor)
        self.filem.add_command(label="Open...", underline=0,
command=self.openFile)
        self.filem.add_command(label="Save", underline=0,
command=self.saveFile)
        self.filem.add_command(label="Save As...", underline=5,
command=self.saveAsFile)
        self.filem.add_command(label="Close", underline=0,
command=self.close)
        self.filem.add_separator()
        self.filem.add_command(label="Exit", underline=0, command=die)

        self.helpm = Menu(self.bar)
        self.helpm.add_command(label="About", underline=0, command=about)

        self.bar.add_cascade(label="File", underline=0, menu=self.filem)
        self.bar.add_cascade(label="Help", underline=0, menu=self.helpm)
        self.window.config(menu=self.bar)

        self.f = Frame(self.window,width=512)
        self.f.pack(side=TOP, expand=1, fill=BOTH)

        self.sbf = Frame(self.window,width=512)
        self.sbf.pack(side=BOTTOM, expand=0, fill=X)

        self.status = statusBar(self.sbf)
        self.status.pack(side=LEFT, fill=X, expand=1)

        self.st = ScrolledText(self.f,background="white",font="System")
        self.st.pack(side=LEFT, fill=BOTH, expand=1)

        self.statusCol = statusBar(self.sbf)
        self.statusCol.pack(side=RIGHT)
        self.statusCol.set("Col: ?")

        self.statusLine = statusBar(self.sbf)
        self.statusLine.pack(side=RIGHT)
        self.statusLine.set("Ln: ?")

        self.st.bind('<KeyRelease>', self.setLineCol)
        self.st.bind('<ButtonRelease>', self.setLineCol)
        self.st.after_idle(self.setLineCol)

    def setFilename(self, fileName):
        self.fileName = fileName
        self.saved = 1

    def setLineCol(self, event=None):
        line, column = string.split(self.st.index(INSERT), '.')
        self.statusLine.set("Ln: "+ line)
        self.statusCol.set("Col: "+ column)

    def close(self):
        self.window.destroy()

    def openFile(self):
        pl = END
        oname = askopenfilename(filetypes=[("Text files", "*.txt")])
        if len(self.st.get(1.0,END)) == 1:
            if oname:
                for line in fileinput.input(oname):
                    self.st.insert(pl,line)
                self.window.title("PythonPadTk - "+oname)
        else:
            if oname:
                newEditor()
                editorList[len(editorList)-1].window.focus()
                for line in fileinput.input(oname):
                    editorList[len(editorList)-1].st.insert(pl,line)
                editorList[len(editorList)-1].window.title("PythonPadTk -
"+oname)
                editorList[len(editorList)-1].window.focus()

    def saveAsFile(self):
        sname = asksaveasfilename()
        if sname:
            ofp = open(sname,"w")
            ofp.write(self.st.get(1.0,END + "-1c"))
            ofp.flush()
            ofp.close()
            self.setFilename(sname)
            self.saved = 1
            self.window.title("PythonPadTk - "+sname)

    def saveFile(self):
        if not self.fileName:
            self.saveAsFile()
        else:
            ofp = open(self.fileName,"w")
            ofp.write(self.st.get(1.0,END + "-1c"))
            ofp.flush()
            ofp.close()

def newEditor():
    global root
    editorList.append(editor(root))

if __name__ == "__main__":
    root = None
    editorList.append(editor(root))
    root = editorList[0].window
    root.mainloop()