Flashcards Maker

This is how columns in csv file for android app Flashcards Maker must be arranged.

CSV columns:

front back deck_name right total

front: front of flashcard
back: back of flashcard
deck_name: name of the deck to which this row/flashcard belongs
right: score for right answers
total: score for total times this flashcard was shown

How to display incorrectly displayed HTML characters correctly

When I uploaded my html document to the server, it displayed é as é. I tried setting meta-tag, saving file as utf-8 to no avail. Finally I found out that HTTP content type has precedence over meta tag and file encoding. I changed my meta tag and file format to confirm with HTTP content type.

While it is recommended to use utf-8 meta tag, file encoding and http content type, since I didn’t own the server and it sent obscure ISO-8859-1 content type I had to convert my file encoding and meta tag to the same encoding to be able to display the characters correctly.

Android HTML Editor

Android HTML Editor

Tips to use Android HTML Editor productively:

To use this app, you have to write a tag (like “span” without quotes) and then press <></> and it becomes <span>|</span>. You can use < ></> if you want to write attributes as well. <> and </> can be used with or without typing tag-name first. There are 7 buttons and you can scroll them left/right if they don’t fit in your mobile device.

Insert tag directly after text:
If you want to insert la<b>|</b>, type la followed by < followed by b and then press a button to expand tag (<>|</>). This way you don’t have to type space before typing and expanding tag and deleting it afterwards.

Highlight from desired line

Go to the line you want to highlight from and add and remove a letter. Highlighting engine will stop and begin highlighting from  the current line. Make sure beginning of the selected line is content or beginning of tag and not middle of tag, comment etc. Eg: highlighting from line beginning with “is comment–> etc etc” results incorrect highlighting while “<b>Good</b> etc etc” works just fine.

How to make a simple but robust text editor in tkinter

Hello everyone!

I love writing text editors. I have written many in many languages: Java, Javascript and Html, Python, Qt. Today I’ll share with you an example in python 3 and tkinter.

Our text editor will be able to open and save files (obviously). What our text editor does that other examples often don’t is it will ask us to save our changes (if there are any) before creating new file, opening another file or closing the program. We will be writing it in python 3 and tkinter.You can extend it yourself to extend its features.Text Editor


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import Tk, Text, Scrollbar, Menu, messagebox, filedialog, BooleanVar, Checkbutton, Label, Entry, StringVar, Grid, Frame
import os, subprocess, json, string

class Editor():
    def __init__(self, root):
        self.root = root        
        self.TITLE = "Subedi Editor"
        self.file_path = None
        frame = Frame(root)
        self.yscrollbar = Scrollbar(frame, orient="vertical")
        self.editor = Text(frame, yscrollcommand=self.yscrollbar.set)
        self.editor.pack(side="left", fill="both", expand=1)
        self.editor.config( wrap = "word", # use word wrapping
               undo = True, # Tk 8.4 
               width = 80 )        
        self.yscrollbar.pack(side="right", fill="y")
        frame.pack(fill="both", expand=1)

        #instead of closing the window, execute a function
        root.protocol("WM_DELETE_WINDOW", self.file_quit) 

        #create a top level menu
        self.menubar = Menu(root)
        #Menu item File
        filemenu = Menu(self.menubar, tearoff=0)# tearoff = 0 => can't be seperated from window
        filemenu.add_command(label="New", underline=1, command=self.file_new, accelerator="Ctrl+N")
        filemenu.add_command(label="Open...", underline=1, command=self.file_open, accelerator="Ctrl+O")
        filemenu.add_command(label="Save", underline=1, command=self.file_save, accelerator="Ctrl+S")
        filemenu.add_command(label="Save As...", underline=5, command=self.file_save_as, accelerator="Ctrl+Alt+S")
        filemenu.add_command(label="Exit", underline=2, command=self.file_quit, accelerator="Alt+F4")
        self.menubar.add_cascade(label="File", underline=0, menu=filemenu)        
        # display the menu
    def save_if_modified(self, event=None):
        if self.editor.edit_modified(): #modified
            response = messagebox.askyesnocancel("Save?", "This document has been modified. Do you want to save changes?") #yes = True, no = False, cancel = None
            if response: #yes/save
                result = self.file_save()
                if result == "saved": #saved
                    return True
                else: #save cancelled
                    return None
                return response #None = cancel/abort, False = no/discard
        else: #not modified
            return True
    def file_new(self, event=None):
        result = self.save_if_modified()
        if result != None: #None => Aborted or Save cancelled, False => Discarded, True = Saved or Not modified
            self.editor.delete(1.0, "end")
            self.file_path = None

    def file_open(self, event=None, filepath=None):
        result = self.save_if_modified()
        if result != None: #None => Aborted or Save cancelled, False => Discarded, True = Saved or Not modified
            if filepath == None:
                filepath = filedialog.askopenfilename()
            if filepath != None  and filepath != '':
                with open(filepath, encoding="utf-8") as f:
                    fileContents = f.read()# Get all the text from file.           
                # Set current text to file contents
                self.editor.delete(1.0, "end")
                self.editor.insert(1.0, fileContents)
                self.file_path = filepath

    def file_save(self, event=None):
        if self.file_path == None:
            result = self.file_save_as()
            result = self.file_save_as(filepath=self.file_path)
        return result

    def file_save_as(self, event=None, filepath=None):
        if filepath == None:
            filepath = filedialog.asksaveasfilename(filetypes=(('Text files', '*.txt'), ('Python files', '*.py *.pyw'), ('All files', '*.*'))) #defaultextension='.txt'
            with open(filepath, 'wb') as f:
                text = self.editor.get(1.0, "end-1c")
                f.write(bytes(text, 'UTF-8'))
                self.file_path = filepath
                return "saved"
        except FileNotFoundError:
            return "cancelled"

    def file_quit(self, event=None):
        result = self.save_if_modified()
        if result != None: #None => Aborted or Save cancelled, False => Discarded, True = Saved or Not modified
            self.root.destroy() #sys.exit(0)

    def set_title(self, event=None):
        if self.file_path != None:
            title = os.path.basename(self.file_path)
            title = "Untitled"
        self.root.title(title + " - " + self.TITLE)
    def undo(self, event=None):
    def redo(self, event=None):

    def main(self, event=None):          
        self.editor.bind("<Control-o>", self.file_open)
        self.editor.bind("<Control-O>", self.file_open)
        self.editor.bind("<Control-S>", self.file_save)
        self.editor.bind("<Control-s>", self.file_save)
        self.editor.bind("<Control-y>", self.redo)
        self.editor.bind("<Control-Y>", self.redo)
        self.editor.bind("<Control-Z>", self.undo)
        self.editor.bind("<Control-z>", self.undo)

if __name__ == "__main__":
    root = Tk()
    editor = Editor(root)

Wordpress doesn’t allow python file uploads. Save this file with extension .pyw or .py.

This is very basic and simple code. If you have any problems running or understanding it, let me know in the comments. Also your feedback is welcome.

You can watch video about my more advanced text editor here: https://www.youtube.com/watch?v=mEpEpWQCPPo
You can download its code in GitHub: https://github.com/Suresh-Subedi/subedi_text_editor_tkinter

Python 3 tkinter Text Editor with Syntax Highlighting

tkinter Text is a powerful widget. Yet we don’t see many popular programs written in it. There was a web browser implemented in it (I have tried to find it back again without success. If someone knows where to find it, please let me know.). IDLE is dismissed as amateur’s IDE. It is a shame that such an easy to use library is not utilized fully.

I love Python, tkinter. Actually this is my third text editor in Python 3 tkinter. I have hosted two of them in GitHub and made video about one of them in youtube. This one is not published anywhere as of yet.

Sometimes I get confused between my editor and IDLE if just for the look of syntax highlighting. As of now it highlights much of Python syntax correctly. What I still need to do is: implement live syntax-highlighting, display line-numbers, implement folding.

Syntax highlighting for multiple languages
Search bar instead of dialog
Expand tag function: type div, press tab, it becomes
<div >|</div > (it inserts div tags with cursor inbetween, ignore space in div tag, it’s due to wordpress formating)
where “|” is your cursor.

Planned features:
(Compile and) run file
Live syntax highlighting
List of methods/functions etc.