################################ # PyPad v.0.1 by Ambigioz # # Contact me at pbanfis@hotmail.it for bugs or other # ################################ #importo i vari moduli from Tkinter import * from SimpleDialog import SimpleDialog from tkSimpleDialog import askstring import tkFileDialog import tkMessageBox from string import * import Tkconstants def chiusura(): answer_question = tkMessageBox.askquestion("PyPad", "Do you really want to exit?", icon=tkMessageBox.WARNING, type=tkMessageBox.YESNO) if answer_question== 'no': return if answer_question== 'yes': root.destroy() else: root.destroy() #Definisco le caratteristiche della finestra root = Tk() root.protocol("WM_DELETE_WINDOW", chiusura) root.title("PythonPad") root.geometry("750x480") scrollbar = Scrollbar(root) scrollbar.pack(side=RIGHT, fill=Y) #Funzioni varie del notepad def rightclick(event): #Popup del tasto destro try: popup.tk_popup(event.x_root, event.y_root, 0) finally: popup.grab_release() def About(): about=Toplevel() about.grab_set() about.title('Pypad - About') about_text=Label(about,text="Python Pad V.1.0\nMade with tkinter and python\nContact me at pbanfis@hotmail.it for bugs and other\n") about_text.pack() about_ok = Button(about,text=" Ok ",command=about.destroy) about_ok.pack() def seleziona_tutto(): txt.tag_add(SEL, '1.0', END) def write(file): testo = txt.get('1.0', END) try: file = open(file, 'w') file.write(testo.encode('utf-8')) file.close() except IOError, m: tkMessageBox.showerror("Write error", m) def salva_con_nome(): dialogo= tkFileDialog.SaveAs(initialfile="Untitled",defaultextension="txt") file = dialogo.show() if len(file): global current_file current_file = file write(file) def save(): try: current_file except NameError: salva_con_nome() else: write(current_file) def new(): testo_apri = txt.get('1.0', END) if len(testo_apri) > 1: answer_question = tkMessageBox.askquestion("PyPad", "The text in untitled file has changed\nDo you want to save changes?", icon=tkMessageBox.WARNING, type=tkMessageBox.YESNOCANCEL) if answer_question== 'no': txt.delete('1.0', END) if answer_question== 'yes': salva() txt.delete('1.0', END) if answer_question== 'cancel': return else: return def apri(): testo_apri = txt.get('1.0', END) if len(testo_apri) > 1: answer_question = tkMessageBox.askquestion("PyPad", "The text in untitled file has changed\nDo you want to save changes?", icon=tkMessageBox.WARNING, type=tkMessageBox.YESNOCANCEL) if answer_question== 'no': apri_file() if answer_question== 'yes': salva() apri_file() if answer_question== 'cancel': return else: apri_file() def apri_file(): s = tkFileDialog.askopenfile(mode='r') file_aperto = s.read() txt.delete('1.0', END) txt.insert('1.0', file_aperto) def taglia(): root.clipboard_clear() testo_tagliare = txt.get(SEL_FIRST, SEL_LAST) txt.delete(SEL_FIRST, SEL_LAST) root.clipboard_append(testo_tagliare) def copia(): root.clipboard_clear() testo_copiare = txt.get(SEL_FIRST, SEL_LAST) root.clipboard_append(testo_copiare) def incolla(): testo_incolla = root.selection_get(selection='CLIPBOARD') txt.insert(INSERT, testo_incolla) def delete(): txt.delete(SEL_FIRST, SEL_LAST) def cerca_popup(): global top top = Toplevel() top.grab_set() top.title('Pypad - Find text') top.geometry("150x120") top.lift(aboveThis=root) top.focus() lab = Label(top,text= "Find what?") lab.pack() global find_text find_text = Entry(top) find_text.pack() find_text.focus() find_but = Button(top,text="Find next",command=cerca) find_but.pack(side="left",expand=1) canc_button = Button(top,text="Cancel",command=top.destroy) canc_button.pack(side="left",expand=1) def cerca(): testo_cercar = find_text.get() if testo_cercar: posizione = txt.search(testo_cercar, INSERT) if posizione: pastit = posizione +('+%dc' % len(testo_cercar)) txt.tag_add(SEL, posizione, pastit) txt.mark_set(INSERT, pastit) txt.see(INSERT) txt.focus() else: tkMessageBox.showwarning( "Find text", "Cannot find text" ) def red(): root.tk_setPalette('#FF0000') def green(): root.tk_setPalette('#00CC00') def gold(): root.tk_setPalette('#FFCC00') def blue(): root.tk_setPalette('#3333FF') def pink(): root.tk_setPalette('#FF33FF') def comic(): txt.config(font=('comic sans ms' ,9, 'normal')) global fontsel fontsel='comic sans ms' def verdana(): txt.config(font=('verdana' ,9, 'normal')) global fontsel fontsel='verdana' def arial(): txt.config(font=('arial' ,9, 'normal')) global fontsel fontsel='arial' def papyrus(): txt.config(font=('papyrus' ,9, 'normal')) global fontsel fontsel='papyrus' def corsiva(): txt.config(font=('Monotype Corsiva' ,9, 'normal')) global fontsel fontsel='Monotype Corsiva' def s12(): txt.config(font=(fontsel ,12, 'normal')) def s10(): txt.config(font=(fontsel ,10, 'normal')) def s8(): txt.config(font=(fontsel ,8, 'normal')) def s6(): txt.config(font=(fontsel ,6, 'normal')) def s14(): txt.config(font=(fontsel ,14, 'normal')) def s16(): txt.config(font=(fontsel ,16, 'normal')) def s18(): txt.config(font=(fontsel ,18, 'normal')) def s20(): txt.config(font=(fontsel ,20, 'normal')) #TEXT WIDGET txt=Text(root,width=150,height=90,yscrollcommand=scrollbar.set) txt.focus( ) txt.pack(expand=YES,fill=BOTH) global fontsel fontsel='' scrollbar.config(command=txt.yview) #MENU SUPERIORE menu1 = Menu(root) menu_File = Menu(menu1, tearoff=0) menu1.add_cascade(label="File", menu=menu_File) menu_File.add_command(label="New",command=new) menu_File.add_command(label="Open...",command=apri) menu_File.add_command(label="Save",command=save) menu_File.add_command(label="Save as...",command=salva_con_nome) menu_File.add_separator() menu_File.add_command(label="Exit", command=root.quit) menu_edit = Menu(menu1, tearoff=0) menu1.add_cascade(label="Edit", menu=menu_edit) menu_edit.add_command(label="Copy CTRL+C",command=copia) menu_edit.add_command(label="Paste CTRL+V",command=incolla) menu_edit.add_command(label="Cut CTRL+X",command=taglia) menu_edit.add_command(label="Delete Canc",command=delete) menu_edit.add_separator() menu_edit.add_command(label="Find...",command=cerca_popup) menu_edit.add_separator() menu_edit.add_command(label="Select all ",command=seleziona_tutto) menu_preferences = Menu(menu1, tearoff=0) menu1.add_cascade(label="Preferences", menu=menu_preferences) menu_color = Menu(menu_preferences,tearoff=0) menu_preferences.add_cascade(label="PyPad color",menu=menu_color) menu_color.add_radiobutton(label="Red",command=red) menu_color.add_radiobutton(label="Green",command=green) menu_color.add_radiobutton(label="Gold",command=gold) menu_color.add_radiobutton(label="Blue",command=blue) menu_color.add_radiobutton(label="Pink",command=pink) menu_font = Menu(menu_preferences,tearoff=0) menu_preferences.add_cascade(label="Font type",menu=menu_font) menu_font.add_radiobutton(label="Comic sans ms",command=comic) menu_font.add_radiobutton(label="Verdana",command=verdana) menu_font.add_radiobutton(label="Arial",command=arial) menu_font.add_radiobutton(label="Papyrus",command=papyrus) menu_font.add_radiobutton(label="Monotype Corsiva",command=corsiva) menu_size = Menu(menu_preferences,tearoff=0) menu_preferences.add_cascade(label="Font size",menu=menu_size) menu_size.add_radiobutton(label="6",command=s6) menu_size.add_radiobutton(label="8",command=s8) menu_size.add_radiobutton(label="10",command=s10) menu_size.add_radiobutton(label="12",command=s12) menu_size.add_radiobutton(label="14",command=s14) menu_size.add_radiobutton(label="16",command=s16) menu_size.add_radiobutton(label="18",command=s18) menu_size.add_radiobutton(label="20",command=s20) menu_help = Menu(menu1,tearoff=0) menu1.add_cascade(label="Help", menu=menu_help) menu_help.add_command(label="About PythonPad",command=About) popup = Menu(root, tearoff=0) popup.add_command(label="Cut",command=taglia) popup.add_command(label="Copy",command=copia) popup.add_command(label="Paste",command=incolla) popup.add_command(label="Delete",command=delete) popup.add_separator() popup.add_command(label="Select all",command=seleziona_tutto) root.bind("", rightclick) #FINE MENU SUPERIORE root.config(menu=menu1) root.mainloop()