Python Tkinter Notepad

Posted by Administrator (admin) on Jun 02 2009
Fun with Code >>

The Code: (Please note

#!user/bin/python
from Tkinter import *
from ScrolledText import ScrolledText
import tkMessageBox
import tkFileDialog

## create new file to directory
def newfile():
    main()

## on save move values to content var
## def onsave():

## save file to directory
def savefile():
    if file_name_ext:
    path_name = tkFileDialog.asksaveasfilename(title="Save File", defaultextension=file_name_ext, filetypes=[("Txt Files",".txt"),("Python Files",".py"),("All Files",".*")])
    else:
    path_name = tkFileDialog.asksaveasfilename(title="Save File", defaultextension=".txt", filetypes=[("Txt Files",".txt"),("Python Files",".py"),("All Files",".*")])
    if path_name:
        file_save = open(path_name, "w")
        content = text.get(1.0, END)
        file_save.write(content)
        file_save.close()
        tkMessageBox.showinfo("Success!", "Your file has been saved!")
    else:
    return 0
    print "Save button clicked!"

## open file from directory
def openfile():
    file_name = tkFileDialog.askopenfile(title="Open File", filetypes=[("Txt Files",".txt"),("Python Files",".py"),("All Files",".*")])
    content = text.get(1.0, END)   
    if file_name and content != '':
        try:
            file_open = file_name.read()
        root.destroy()
            main(file_open, file_name)
        file_name.close()
            print "Open button clicked!"
    except Exception, e:
        showwarning("File Problem", "Couldn't read file '%s': %s" % (file_name, str(e)))
    elif file_name:
        file_open = file_name.read()
    root.destroy()
        main(file_open, file_name)
    file_name.close()
        print "Open button clicked!"
    print "Cancel button clicked!"
   
## close the application
def exitapp():
    exit_app = tkMessageBox.askokcancel("Exit Isiaha's Notepad", "You are about to exit this program, do you wish to continue?")
    if exit_app == True:
        root.destroy()
    print "Ok button clicked!"
    print "Cancel button clicked!"

## close the application
def callback():
    print "Callback selected!"

def setfontplus():
    return font_size.get()+1
    print "font_var: ", font_size.get()

def setfontminus():
    return font_size.get()-1
    print "font_var: ", font_size.get()
   
## file menu
def filemenu(menu_frame):
    file_menu = Menu(menu_frame, tearoff=0)
    file_menu.add_command(label="Create new file", command=newfile)
    file_menu.add_separator()
    file_menu.add_command(label="Open", command=openfile)
    file_menu.add_command(label="Save", command=savefile)
    file_menu.add_separator()
    file_menu.add_command(label="Exit", command=exitapp)
    menu_frame.add_cascade(label="File", menu=file_menu)

## edit menu
def editmenu(menu_frame):
    edit_menu = Menu(menu_frame, tearoff=0)
    edit_menu.add_command(label="Cut", command=callback)
    edit_menu.add_command(label="Copy", command=callback)
    edit_menu.add_command(label="Paste", command=callback)
    menu_frame.add_cascade(label="Edit", menu=edit_menu)

## font menu
def fontmenu(menu_frame):
    font_menu = Menu(menu_frame, tearoff=0)
    font_menu.add_command(label="Increase", command=setfontplus)
    font_menu.add_command(label="Decrese", command=setfontminus)
    menu_frame.add_cascade(label="Font", menu=font_menu)
   
## main function
def main(var=None, option=None):

    ## globals
    global root, text, file_name_ext, font_var, font_size
   
    ## set vars
    root = Tk()
    content = StringVar()
    font_size = IntVar()
    font_var = IntVar()
    font_size = 10

    text = Text(root, relief=SUNKEN, font=("Courier", font_size))
    scroll_bar = Scrollbar(root)

    ## set frame
    text.delete(1.0, END)   
    text.config(yscrollcommand=scroll_bar.set)          
    text.pack(side=LEFT, expand=YES, fill=BOTH) 

    ## set scrollbar frame
    scroll_bar.config(command=text.yview)              
    scroll_bar.pack(side=RIGHT, fill=Y)   
   
    ## main menu frame
    menu_frame = Menu(root)
    filemenu(menu_frame)
    editmenu(menu_frame)
    fontmenu(menu_frame)
   
    ## check to see if var is set
    if var != None:
        root.title(option.name)
        root.config(menu=menu_frame)
        text.insert(END, var)
        file_name_ext = option.name
        print "var output: ", var, " option output: ", option.name
       
    else:
        root.title("Untitled")
        root.config(menu=menu_frame)

    ## set mainloop
    root.mainloop()

## start main function
main()

## end of application
print "Application closed!"
 

 

Last changed: Jun 01 2009 at 11:36 PM

Back
 
 


Comments

None Found

Add Comment