11

In python-3.x with tkinter GUI, I developed a program with a regular simple window.

I want to show a markdown format string saved in a string called markdownText on program window:

markdownText='_italic_ or **bold**'

desired output is:

italic or bold

Is there any solution?

Sadegh
  • 865
  • 1
  • 23
  • 47
  • any idea is welcome... – Sadegh Mar 01 '16 at 22:51
  • Are italicized/bold strings always on a single line or can they span multiple lines? Either way you need to use the `Text` widget, but the multiple-line case requires more work. – gil Mar 01 '16 at 23:00
  • It is multi-line with hyperlinks ... I am not looking for a complicated solution. so I suppose there is no solution, – Sadegh Mar 01 '16 at 23:02
  • Maybe other GUI has a direct solution for markdown format? do you know? – Sadegh Mar 01 '16 at 23:06
  • 1
    No, I don't think there is a very easy one with pure `tkinter`. You write your own lexer, find where the marked parts are and apply tags... That's how I'd do it, anyway. Maybe 40 lines of code for just italics and bold. – gil Mar 01 '16 at 23:06
  • Sorry I'm not familiar with other GUI frameworks. – gil Mar 01 '16 at 23:08
  • Do you want to show the formatted text in the same widget where you edit the text, or would the formatted text be in a separate widget from where the text is edited? The answer is very different and it is not clear to me which answer you are looking for. – Waylan Mar 02 '16 at 00:34
  • @Waylan separate widget. I just want to show markdown text and there is no need for user to modify it. – Sadegh Mar 02 '16 at 08:54
  • Maybe I can convert markdown to html or other suitable format and then convert it to tkinter ? – Sadegh Mar 02 '16 at 10:18
  • Okay, it you just want to display the text, convert it to HTML and display that. Maybe embed a browser window into your app and have that display the HTML. I'm not familiar enough with tkinter to offer any more, but that is the method I would use on any GUI framework. – Waylan Mar 02 '16 at 14:11
  • @Waylan I will do this approach if there is no straight forward solution. btw it is strange there is no other questions with both markdown and tikinter together : http://stackoverflow.com/questions/tagged/tkinter+markdown – Sadegh Mar 02 '16 at 19:23

1 Answers1

10

I was just searching for a similar solution, and it does indeed not seem like there is a default module/class/library for the combination of Python, TkInter and markdown. However a continued search revealed the following options:

  • Python-Markdown – This a module capable of parsing markdown into the following output formats: Python-Markdown can output documents in HTML4, XHTML and HTML5.
  • TkInter displaying html – As the linked answer indicates, Tkhtml can display html "pretty well", and it has a python wrapper

In other words, if you are willing to use an intermediate step of converting into html that might be a viable route for you to display markdown strings within a tkinter GUI.

Stypox
  • 963
  • 11
  • 18
holroy
  • 3,047
  • 25
  • 41