Tuesday, 9 July 2013

Creating Notepad in Python

Hey guys,

I am going to tell you how to make note pad in Python Tkinter

Here i have implemented File and Edit options.

Here i have used flag option to check whether file is changed or not.

Code:


from Tkinter import *
import Tix
import tkFileDialog
import tkMessageBox
import tkColorChooser
from FontChooser import *

class App:
def callback():
    print "click!"


def onClose(self):
    # check if saving
    if(self.flag==1):
self.flag=0;
    self.root.destroy()
else:
f=Tk()
f.minsize(width=450,height=50)
f.title("Please save your work or quit")

label = Label(f, text="DO you want to make any changes to current file?")


label.pack()

#b = Button(f, text="OK", command=callback)
#b.pack()

        myYesButton = Button(f,text='Yes', command=self.doSaveAs)
myNoButton = Button(f,text='No', command=self.doQuit)
myYesButton.pack()
myNoButton.pack()
f.mainloop()
#myNoButton = Button(frame,text='Cancel', command=self.doQuit(self))
       
       

def __init__(self):
       # Set up the screen, the title, and the size.
self.fileOp=0
        self.root = Tk()
self.flag=0
        self.root.title("Untitled")
        self.root.minsize(width=500,height=400)
self.curr_var = StringVar(value='-:-')

self.root.protocol('WM_DELETE_WINDOW', self.onClose)

             
       # Set up basic Menu
        menubar = Menu(self.root)
# searching
       # Set up a separate menu that is a child of the main menu
        filemenu = Menu(menubar,tearoff=0)
editMenu = Menu(menubar,tearoff=0)
        filemenu.add_command(label="New File", command=self.doNew,
                            accelerator="Ctrl+N")

       # Try out openDialog
        filemenu.add_command(label="Open", command=self.doOpen,
                            accelerator="Ctrl+O")

       # Try out the save
        filemenu.add_command(label="Save", command=self.doSave,
                            accelerator="Ctrl+Shift+S")
       # Try out the saveAsDialog
        filemenu.add_command(label="Save As", command=self.doSaveAs,
                            accelerator="Ctrl+Shift+S")
       # Try out the Quit
        filemenu.add_command(label="Quit", command=self.doQuit,
                            accelerator="Ctrl+Q")
       

menubar.add_cascade(label="File", menu=filemenu)
       # Setting up edit menu
editMenu.add_command(label="Undo",command=self.doUndo, accelerator="Ctrl+z")
# Try out Redo
editMenu.add_command(label="Redo",command=self.doRedo, accelerator="Shift+Ctrl+z")
# Try out cut
editMenu.add_command(label="cut",command=self.doCut, accelerator="Ctrl+X")
       

# Try out copy
editMenu.add_command(label="copy",command=self.doCopy, accelerator="Ctrl+c")
       

# Try out paste
editMenu.add_command(label="paste",command=self.doPaste, accelerator="Ctrl+v")

      # Try out Select all
editMenu.add_command(label="Select All",command=self.doSelectAll, accelerator="Ctrl+A")

# Try out delete
editMenu.add_command(label="delete",command=self.doDelete, accelerator="Del")
# Try out color chooser
editMenu.add_command(label="Color chooser",command=self.colorChooser)
#Try out Font Chooser
editMenu.add_command(label="Font chooser",command=self.fontChooser)
menubar.add_cascade(label="Edit", menu=editMenu)
        self.root.config(menu=menubar)

       # Set up the text widget
        self.text = Text(self.root)
        self.text.pack(expand=YES, fill=BOTH) # Expand to fit
                                # vertically and horizontally
def doNew(self):
    # Clear the text
self.fileOp=0
self.root.title("Untitled")
    self.text.delete(0.0, END)
def doQuit(self):
    # Exiting from notepad
import sys; sys.exit()
    self.text.delete(0.0, END)
def doUndo(self):
    # Clear the text
    self.text.delete(0.0, END)
def doRedo(self):
    # Clear the text
    self.text.delete(0.0, END)
def doCut(self):
    # cuts the text and saves it into buffer.txt file for pasting

try:
text=Text(self.root)
content =text.selection_get()
if(content):
buf=open("buffer.txt","w")
buf.write(content)
buf.close()
self.text.delete("sel.first", "sel.last")
except IOError:
print "Noting is selected for copying"
   
def doDelete(self):
    # Deletes the text which is selected
text=Text(self.root)
    self.text.delete("sel.first", "sel.last")

def doSelectAll(self):
#Selects all text in file
print "here"
self.text.tag_add("sel","1.0","end")
def doCopy(self):
    # copy text to buffer.txt
try:
text=Text(self.root)
content =text.selection_get()
if(content):
buf=open("buffer.txt","w")
   
buf.write(content)
buf.close()
except IOError:
print "Noting is selected for copying"
def doPaste(self):
    # Paste the text from the file which have saved copied content buffer.txt    
try:
buf=open("buffer.txt","r")
content=buf.read();
buf.close()
self.text.insert('insert', content)
except IOError:
print "no contents for pasting"


def doSaveAs(self):
    # Returns the saved file
    file = tkFileDialog.asksaveasfile(mode='w')
    textoutput = self.text.get(0.0, END) # Gets all the text in the field
    file.write(textoutput.rstrip()) # With blank perameters, this cuts
                             # off all whitespace after a line.
    file.write("\n") # Then we add a newline character.
self.fileOp=file.name
self.root.title(file.name)
file.close()
self.text.delete(0.0, END)
file=open(self.fileOp,'r')
fileContents = file.read() # Get all the text from file.

    # Set current text to file contents
    self.text.delete(0.0, END)
    self.text.insert(0.0, fileContents)


def doOpen(self):
    # Returns the opened file
    file = tkFileDialog.askopenfile(mode='r')
self.fileOp=file.name
print self.fileOp
self.root.title(file.name)
    fileContents = file.read() # Get all the text from file.

    # Set current text to file contents
    self.text.delete(0.0, END)
    self.text.insert(0.0, fileContents)
def doSave(self):
print self.fileOp
if(self.fileOp!=0):
file=open(self.fileOp,"w")
textoutput = self.text.get(0.0, END)
file.write(textoutput.rstrip())
file.write("\n") # Then we add a newline character.
file.close()
else:
self.doSaveAs()

def colorChooser(self):
color = tkColorChooser.askcolor()
color_name = color[1]    #to pick up the color name in HTML notation, i.e. the 2nd element of the tuple returned by the colorchooser
Text(self.root,fg=color_name)
          def fontChooser(self):
font = callChooseFont()
      print font



app = App()
app.root.mainloop()

No comments:

Post a Comment