[PythonCE] re: Idle

Stewart Midwinter stewart.midwinter at gmail.com
Sun Jan 9 00:15:41 CET 2005


here's the attachment.  Found out I could post wirelessly from my
toshiba e830 via gmane, but you can't post an attachment that way.  I
have to use GMail for that, but it won't recognize Netfront or PIE as
valid browsers; hence, back to the desktop for this posting.

S

-- 
Stewart Midwinter
stewart at midwinter.ca
stewart.midwinter at gmail.com
-------------- next part --------------
title = 'tkPPCedit'

# Import Pmw from this directory tree.
import sys, os
sys.path.append('\\Program Files\\Python\\Lib\\python23.zip\\lib-tk')
from Tkinter import *
sys.path[:0] = ['\\SD Card']
import Pmw
import os, os.path
from tkMessageBox import showerror, showinfo
from tkFileDialog import *


class Editor:
    '''this class creates a scrolled text window that optionally will have a file's contents dumped into it.
       There are also methods for opening a file for editing, and saving it, and also clearing the window.'''
    def __init__(self, parent, location=None, filename=None):
        '''Create and pack the PanedWidget to hold the input window.
            !! panedwidget should automatically size to requested size '''
    
        self.myPath = None
        self.location = location
        self.filename = filename
        #self.myPath = os.path.join(self.location, self.filename)
        if self.filename is None:
            self.myPath = self.location
            self.mytext = 'Not editing a file'
        else:
            self.myPath = self.location+'\\'+self.filename
            self.mytext = 'Now editing %s' % self.myPath
            
        panedWidget = Pmw.PanedWidget(parent,
                orient = 'vertical',
                hull_height = 280,
                hull_width = 220)
        panedWidget.add('input', min = 0.05)
        panedWidget.add('info', min = 0.05, max = 0.05)
        panedWidget.add('buttons', min = 0.1, max = 0.1)
        panedWidget.pack(fill = 'both', expand = 1)
        self.panedWidget = panedWidget

        buttonList = (
            [2, None],
            ['Clear', self.clear],
            ['Open', Pmw.busycallback(self.getFile)],            
            ['Save', Pmw.busycallback(self.saveFile)],            
            ['Save as...', Pmw.busycallback(self.saveasFile)], 
            ['Quit', Pmw.busycallback(self.close)],
            ['About', Pmw.busycallback(self.getabout)],
        )
        self.buttonDict = {}
        '''
            [50, None],
            ['Close', self.close],
        '''
        
        infoFrame = panedWidget.pane('info')
        spacer= Label(infoFrame, width='1', text=' ')
        spacer.pack(side='left', fill='none')
        label = Label(infoFrame, text=self.mytext)
        label.pack(side='left',fill='none')
        self.label = label

        buttonFrame = panedWidget.pane('buttons')        
        for text, cmd in buttonList:
            if type(text) == type(69):
                frame = Frame(buttonFrame, width = text)
                frame.pack(side = 'left')
            else:
                button = Button(buttonFrame, text = text, command = cmd)
                button.pack(side = 'left')
                self.buttonDict[text] = button

        self.input = Pmw.ScrolledText(panedWidget.pane('input'), text_wrap = 'none')
        self.input.pack(fill = 'both', expand = 1)

        '''if we were passed a filename, open file and show contents'''
        if self.location is not None and self.filename is not None:
            #fid = os.path.join(self.location, self.filename)
            fid = os.path.join(self.location, self.filename)
            self.getFile(self.location, self.filename)
            
    def clear(self):
        '''clear out the contents of the window'''
        self.input.delete('1.0', 'end')
        self.mytext = 'Now editing an empty file'
        self.label.config(text=self.mytext)
        self.myPath = None

    def close(self):
        '''close the window that we live in'''
        sys.exit()

    def addnewlines(self, text):
        '''how to handle line endings on inserted text
            this adds extra lines to every piece of text.
            we don't want that, so we'll comment it out.'''
        if len(text) == 1:
            text = text + '\n'
        if text[-1] != '\n':
            text = text + '\n'
        if text[-2] != '\n':
            text = text + '\n'
        return text

    def getFile(self, location=None, filename=None):
        '''open a file'''
        #firstly clear window 
        self.clear()
        if location == None:
            location == "\\My Documents"
        else:
            os.chdir(location)
        if filename == None:
            myPath = askopenfilename(filetypes=[("text files", "*.*")])
            msg = 'selected file is \n' +myPath
            #showinfo('Info', msg)
            myPath = os.path.normpath(myPath)
            if myPath is None or len(myPath) < 1:
                return
        else:
            myPath = location +'\\'+filename
        self.myPath = myPath
        try:
            fid = open(myPath, 'r')
        except IOError:
            showerror('No file', "Sorry, I can't find %s" % myPath)
            return
        lines = fid.readlines()
        fid.close()
        for line in lines:
            #self.input.insert('end', self.addnewlines(line))
            self.input.insert('end', line)
        self.input.see('end')
            
    def saveFile(self):
        '''save a previously opened file'''
        if self.location is not None:
            os.chdir(self.location)
        if self.myPath is None:
            showerror('No file',"I don't know where to save the file!")
            self.saveasFile()
        else:
            fid = open(self.myPath, 'w+')   #truncate
            lines = self.input.get()  #put something in here
            fid.write(lines)
            fid.close()
            msg = 'Saved: %s' % self.myPath 
            showinfo('Saved', msg)
            mytext = "Still editing %s" % self.myPath
            self.label.config(text=mytext)

    def saveasFile(self):
        '''save a file under a new name'''
        if self.location is not None:
            os.chdir(self.location)
        ans = asksaveasfilename()
        if ans is not None and len(ans) > 0:
            self.myPath = ans
            msg = 'selected file is: %s' % self.myPath 
            #showinfo('Info', msg)
            self.myPath = os.path.normpath(self.myPath)
            fid = open(self.myPath, 'w+')   #truncate
            lines = self.input.get()
            fid.write(lines)
            fid.close()
            msg = 'Saved: %s' % self.myPath 
            showinfo('Saved', msg)
            mytext = "Now editing %s" % self.myPath
        else:
            mytext = 'Not editing a file'
        self.label.config(text=mytext)

    def getabout(self):
        ab = About()
        ab.execute()
        #showinfo('', '1')

class About:
    def __init__(self):
        mytext = "tkPPC Editor v.0.1\n(c) Stewart Midwinter\nFreeware: use at your own peril. "
        self.mytext = mytext
        
    def execute(self):
        showinfo('', '2')
        a = Toplevel()
        l = Label(a, text = self.mytext)
        l.pack(side='top')
        b = Button(a, text='Ok', command=a.destroy)
        b.pack(side='bottom')
        
######################################################################

# Create demo in root window for testing.
if __name__ == '__main__':

    location = '\\My Documents'
    filename = None
    root = Tk()
    Pmw.initialise(root)
    root.title(title)
    width=240; height=300; x=0; y=0
    geometry = "%sx%s+%s+%s" %(width,height,x,y)
    root.geometry(geometry)
    '''
    exitButton = Button(root, text = 'Exit', command = root.destroy)
    exitButton.pack(side = 'bottom')
    a = Button(root, text = 'About', command = about())
    a.pack(side = 'left')
    '''
    widget = Editor(root, location, filename)
    root.mainloop()


More information about the PythonCE mailing list