Editor de texto em Tkinter

Publicado por Perfil removido (última atualização em 13/07/2011)

[ Hits: 17.014 ]

Download notepad.py




Esse é um pequeno editor de textos feito em Python. Utiliza a interface gráfica Tkinter.

  



Esconder código-fonte

#! /usr/bin/python
# -*- coding: UTF-8 -*-

from Tkinter import *
from tkFileDialog import asksaveasfilename,askopenfilename


#Começa a classe do editor:
class PyNotePad:
    # Aqui fica a função inicial:
    def __init__(self):
        self.root = Tk()
        self.root.wm_title("PyNotePad")# Aqui é o digito

        # "inicia" a scroolbar
        scrollbar = Scrollbar(self.root)
        scrollbar.pack(side=RIGHT, fill=Y)

        menubar = Menu(self.root)
        #Aqui criamos os menus:
        MENUarquivo = Menu(menubar)
        MENUarquivo.add_command(label="Salvar", command=self.salvar)
        MENUarquivo.add_command(label="Abrir", command=self.abrir)
        menubar.add_cascade(label="Arquivo", menu=MENUarquivo)

        MENUajuda = Menu(menubar)
        MENUajuda.add_command(label="Sobre", command=self.sobre)
        menubar.add_cascade(label="Ajuda", menu=MENUajuda)
        self.root.config(menu=menubar)

        # Aqui adicionamos a parte que fica o texto:
        self.text = Text(self.root)
        self.text.pack(expand=YES, fill=BOTH)

        #aqui configura o scrollbar
        self.text.config(yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.text.yview)


        # Por Fim, a janela:
        self.root.mainloop()

    def salvar(self): # Aqui é a função que salva o arquivo:

        fileName = asksaveasfilename()
        try:
            file = open(fileName, 'w')
            textoutput = self.text.get(0.0, END)
            file.write(textoutput)
        except:
            pass
        finally:
            file.close()

    def abrir(self):# Aqui é a função que abre um arquivo
      

        fileName = askopenfilename()
        try:
            file = open(fileName, 'r')
            contents = file.read()

            self.text.delete(0.0, END)
            self.text.insert(0.0, contents)
        except:
            pass

    def sobre(self):# uma pequena função "sobre" :D
        root = Tk()
        root.wm_title("Sobre")
        texto=("PyNotePad: Versão 1.0")
        textONlabel = Label(root, text=texto)
        textONlabel.pack()

# inicia o programa:
PyNotePad()

Scripts recomendados

Cronômetro com interface gráfico utilizando a biblioteca Tkinter

Utilizando a biblioteca Numpy

Algoritmo genético - rotas

Controle de Depósito com SQLite + Python

O maior quebra cabeças de sempre


  

Comentários
[1] Comentário enviado por removido em 10/01/2011 - 22:06h

Hi, Luciano!

Thanks a lot for the code! It was just what I was looking for. I dared to add some improvements, in the menu, and I thought perhaps you'd like to see how it in its new features.

Best regards,
Enih Gil'ead

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

#! /usr/bin/python
# -*- coding: UTF-8 -*-

# From: http://www.vivaolinux.com.br/script/Editor-de-texto-em-Tkinter

from Tkinter import *
from tkFileDialog import asksaveasfilename,askopenfilename

class PyNotePad: # Start editor's class
def __init__(self):
self.root = Tk()
self.root.wm_title("Enihgil's NotePad")

# Start - scrollbar
scrollbar = Scrollbar(self.root)
scrollbar.pack(side=RIGHT, fill=Y)

menubar = Menu(self.root) # Start - menu
MENUarquivo = Menu(menubar, tearoff=0)
menubar.add_cascade(label=" File ", menu=MENUarquivo)
MENUarquivo.add_separator()
MENUarquivo.add_command(label=" New", command=self.new_text)
MENUarquivo.add_command(label=" Open", command=self.abrir)
MENUarquivo.add_command(label=" Save As", command=self.salvar)
MENUarquivo.add_separator()
MENUarquivo.add_command(label=" Exit", command=self.do_exit)
self.root.config(menu=menubar)

self.text = Text(self.root) # Add the CANVA's text
self.text.pack(expand=YES, fill=BOTH)

self.text.focus() # Puts cursor at the working area

# Configure the scrollbar
self.text.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=self.text.yview)

self.root.mainloop() # Loop's window

def new_text(self): # NEW - clear the text area
self.text.delete(0.0, END)

def abrir(self): # OPEN - file
fileName = askopenfilename()
try:
file = open(fileName, 'r')
contents = file.read()

self.text.delete(0.0, END)
self.text.insert(0.0, contents)
except:
pass

def salvar(self): # SAVE -file
fileName = asksaveasfilename()
try:
file = open(fileName, 'w')
textoutput = self.text.get(0.0, END)
file.write(textoutput)
except:
pass
finally:
file.close()

def do_exit(self): # Exit - the program
self.root.destroy()

PyNotePad() # Starts the program

[2] Comentário enviado por bobthepanda em 29/04/2012 - 18:49h

Thanks for the code! :D


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts