Skip to content

TkintermdFrame

tkintermd.frame.TkintermdFrame

Bases: tk.Frame

A Markdown editor with HTML preview for use in tkinter projects.

The editor has syntax highlighting supplied by Pygments and the HTML preview window is provided by tkinterweb.

Import it into your own scripts like so:

from tkintermd.frame import TkintermdFrame

import tkinter as tk
from tkinter.constants import *

root = tk.Tk()
app = TkintermdFrame(root)
app.pack(fill="both", expand=1)
app.mainloop()

apply_markdown_both_sides(selection, md_syntax)

Apply markdown to both sides of a selection.

Parameters:

Name Type Description Default
selection str

Text selection from the editor.

required
md_syntax tuple

Tuple of markdown strings to apply.

required

change_template(template_name)

Change the currently selected template.

Get the selected template name from the StringVar for the Combobox and compare it with the templates dictionary. If the name matches the key then set the relevant template values and update all the previews.

check_markdown_both_sides(md_syntax, md_ignore, md_special, strikethrough=None)

Check markdown formatting to be applied to both sides of a selection.

This will ignore items in the md_ignore variable and then deal with special syntax individually before applying or removing the markdown formatting.

  • If string starts with anything in md_ignore do nothing and return from the function.
  • If strikethrough is set to True then apply or remove the markdown.
  • If the formatting requires special items which can't go in md_ignore because they cause issues with markdown being applied incorrectly do nothing and return from the function.
  • Apply or remove the markdown once we reach the end.

Parameters:

Name Type Description Default
selection str

Text selection from the editor.

required
md_syntax tuple

Tuple of markdown strings to remove.

required
md_ignore tuple

Tuple of markdown strings to ignore.

required
md_special tuple

Tuple of special markdown strings to ignore that cause unexpected issues when included in md_ignore.

required
strikethrough bool

Set to True for strikethrough. Default is None.

None

check_markdown_highlighting(start='insert linestart', end='insert lineend')

Formats editor content using the Pygments style.

enable_edit()

Enable editing of HTML before export.

Displays a warning to the user and enables HTML editing prior to export.

find(*args)

Simple search dialog for within the editor window.

  • Get the current text area content.
  • Displays a simple dialog with a field to enter a search string into and two buttons.
  • If a string is provided then search for it within the text area.
  • Add tag TAGNAME to all characters between INDEX1 and INDEX2.
  • Highlight any found strings within the text editor.

load_style(stylename)

Load Pygments style for syntax highlighting within the editor.

  • Load and configure the text area and tags with the Pygments styles and custom Lexer tags.
  • Create the CSS styling to be merged with the HTML template
  • Generate a <<Modified>> event to update the styles when a new style is chosen.

on_input_change(event)

Converts the text area input into html output for the HTML preview.

When the user types:

  • Get the current text area contents.
  • Convert the markdown formatted string to HTML.
  • Merge the converted markdown with the currently selected template.
  • Load the merged HTML into the document preview.
  • Update the HTML content/states within the export options edit area.
  • Check the markdown and apply formatting to the text area.
  • Reset the modified flag.

open_md_file()

Open a file and clear/insert the text into the text_area.

Opens a native OS dialog and expects markdown formatted files. Shows an error message if it fails.

  • Display a native OS dialog and request a filename to open.
  • If a filename is provided then try to open it in "read" mode.
  • Replace the text area content.
  • Set the constants.cur_file value to the filename that was opened.
  • If any of the above fails then display an error message.

popup(event)

Right-click popup at mouse location within the text area only.

Provides the following options:

  • Cut.
  • Copy.
  • Paste.
  • Undo.
  • Redo.
  • Find.
  • Select All.

remove_markdown_both_sides(selection, md_syntax)

Remove markdown from both sides of a selection.

Parameters:

Name Type Description Default
selection str

Text selection from the editor.

required
md_syntax tuple

Tuple of markdown strings to remove.

required

save_as_html_file()

Exports the current contents of the HTML preview pane to the given filename.

Opens a native OS dialog for saving the file with a html extension. Shows an error message if it fails.

  • Display a native OS dialog and request a filename to save.
  • If a filename is provided then try to open it in "write" mode.
  • If any of the above fails then display an error message.

save_as_md_file()

Saves the file with the given filename.

Opens a native OS dialog for saving the file with a name and markdown extension. Shows an error message if it fails.

  • Display a native OS dialog and request a filename to save.
  • If a filename is provided then try to open it in "write" mode.
  • Set the constants.cur_file value to the filename that was opened.
  • If any of the above fails then display an error message.

save_md_file()

Quick saves the file with its current name.

  • Get the current text area content.
  • try to save the file with the constants.cur_file variable.
  • If it fails because no name exists it calls the "save_as_md_file" function.

select_all(*args)

Select all text within the editor window.

  • Add tag TAGNAME to all characters between INDEX1 and INDEX2.
  • Set mark MARKNAME before the character at index.
  • Scroll so that the character at INDEX is visible.

Last update: 2023-09-26
Back to top