Tuesday, 9 July 2013

Links for Learning Python and Python Tkinter

Hey guys,



Here are the some useful links to learn Python

Links:


http://stackoverflow.com/questions/13068043/python-syntaxerror-return-outside-function

http://www.tutorialspoint.com/python/

http://zetcode.com/db/postgresqlpythontutorial/

http://docs.python.org/2/library/sqlite3.html

http://stackoverflow.com/questions/1219326/how-do-i-do-database-transactions-with-psycopg2-python-db-api

http://docs.python.org/2/index.html

http://scipy.org/install.html

http://www.dreamincode.net/forums/forum/133-python-tutorials/

http://www.python-course.eu/tkinter_events_binds.php

http://www.tkdocs.com/tutorial/text.html

http://stackoverflow.com/questions/17055166/how-do-i-get-the-indices-from-the-tkinter-textbox

http://stackoverflow.com/questions/16383530/python-return-current-cursor-position-and-position-last-left-mouse-click

http://stackoverflow.com/questions/6950007/pasting-in-the-tkinter-text-widget

http://mail.python.org/pipermail/tkinter-discuss/2006-October/000911.html

http://stackoverflow.com/questions/5322027/how-to-erase-everything-from-the-tkinter-text-widget

http://stackoverflow.com/questions/3781670/tkinter-text-highlighting-in-python

http://stackoverflow.com/questions/4073468/how-do-i-get-a-selected-string-in-from-a-tkinter-text-box

http://stackoverflow.com/questions/8144468/im-trying-to-create-a-button-in-python-importing-tkinter-that-
changes-color-a?rq=1

http://shop.oreilly.com/product/9780596001674.do?sortby=bestSellers

http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/

http://effbot.org/tkinterbook/tkinter-index.htm#class-reference

http://stackoverflow.com/questions/3501849/how-to-bind-self-events-in-tkinter-text-widget-after-it-will-binded-by-text-widg

http://stackoverflow.com/questions/9691205/tkinter-text-widget-highlight-the-line-containing-the-cursor

http://tkinter.unpythonic.net/wiki/FontChooser

http://stackoverflow.com/questions/5870863/python-tkinter-font-chooser

http://stackoverflow.com/questions/2744795/background-color-for-tk-in-python

http://stackoverflow.com/questions/6865792/fresh-tutorial-on-tkinter-and-ttk-python-3

http://stackoverflow.com/questions/2260235/how-to-clear-the-entry-widget-in-a-gui-after-a-button-is-pressed-in-

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()

Creating Custom JSP Tags

Hi,


You people all know how to create custom tags in class file using doStart,doEnd functions

Its cumbersome to use all that function and we have to define custom tags in web.xml also

Here i am going to show a simple method to create custom(User defined Tags)

Let me tell you for what purpose custom tags are used
  • To implement business logic
  • To hide important functionality
  • Code reuse
We get all the 9 default objects of jsp's in custom tag body. So we can access everything which is there in JSP. So get used to custom tags it will help u alot 
 Steps to create Custom tags
  1. Create a folder named "tags" in web-inf
  2. Right click on "tags" folder new->others->JSP Tag
Steps to use Custom tag in JSP file
  1. Have to add <%@taglib %> in that 
  2. Use "uri" attribute give path of "tags" folder 
  3. Use attribute "prefix" to use custom tag