Hi, im new to this forum, im trying to make a text editor in python for my school, and its nearly finished, but whenever i save a text file then open it again it makes weird square symbols at the end of each line. Thanks in advance. And i didn't use classes cause i don't know HOW to use them, and they seem long and weird. Here's my code:

#coding=utf8
import Tkinter as tk
from Tkinter import *
from ScrolledText import *
import tkFileDialog
import tkMessageBox
def small_command():
    textPad.config(font="Courier 6")
def medium_command():
    textPad.config(font="Courier 9")
def large_command():
    textPad.config(font="Courier 12")
def open_command():
    file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Open',)
    if file != None:
        contents = file.read()
        textPad.delete('1.0', END+'-1c')
        textPad.insert('1.0',contents)
        file.close()
        textPad.mark_set(INSERT, 1.0)
def save_command():
    file = tkFileDialog.asksaveasfile(parent=root,mode='w',title='Save',defaultextension=".txt")
    if file != None:
        data = textPad.get('1.0', END+'-1c')
        file.write(data.rstrip())
        file.write("\n")           
        file.close()
def new_command():
    textPad.delete('1.0', END+'-1c')
def exit_command():
    exit()
def copy_command():
    root.clipboard_clear()
    text = textPad.get("sel.first", "sel.last")
    root.clipboard_append(text)
def paste_command():
    text = root.selection_get(selection='CLIPBOARD')
    textPad.insert('insert', text) 
def cut_command():
    root.clipboard_clear()
    text = textPad.get("sel.first", "sel.last")
    root.clipboard_append(text)
    textPad.delete("sel.first", "sel.last")
root = tk.Tk(className=" Pytext")
textPad = ScrolledText(root, width=100, height=80)
menu = tk.Menu(root)
root.config(menu=menu)
filemenu = tk.Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=new_command)
filemenu.add_command(label="Open...", command=open_command)
filemenu.add_command(label="Save...", command=save_command)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=exit_command)
editmenu = tk.Menu(menu)
menu.add_cascade(label="Edit", menu=editmenu)
editmenu.add_command(label="Cut", command=cut_command)
editmenu.add_command(label="Copy", command=copy_command)
editmenu.add_command(label="Paste", command=paste_command)
fontmenu = tk.Menu(menu)
menu.add_cascade(label="Font", menu=fontmenu)
fontmenu.add_command(label="Small", command=small_command)
fontmenu.add_command(label="Medium", command=medium_command)
fontmenu.add_command(label="Large", command=large_command)
textPad.config(font="Courier 9")
textPad.pack()
textPad.config(wrap=WORD)
root.mainloop() 

Recommended Answers

All 2 Replies

I could not reproduce the bug on my system. Considering that you read the file with mode 'rb', perhaps you should write it in mode 'wb'.

Ok. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.