Introduction to tkinter rich text tkr(1)

introduction

Everyone knows that tkinter is the GUI library officially used by Python. Regarding the advantages and disadvantages of tkinter, I will not say much here. This column mainly discusses and studies the native rich text format of tkinter, so as to create a rich text format file suitable for tkinter itself for tkinter and realize rendering.


Rich text that tkinter can use

Text itself

The Text component is the second most functional component in tkinter after Canvas, and it is also the most convenient component for content layout. Text can display certain rich text content through tag, mark and other methods, which plays a very important role in the development of tkinter interface. However, the disadvantages are also obvious.

  1. Content rendering is also poor in functional code, which is not conducive to specification development
  2. The use of methods is the same as that of functions in Python. If the content to be displayed is complicated, how to code it is also very complicated

For example, to display colored text:

Text.tag_config('colorred',foreground='red',background='black')
Text.insert('end','red words','colorred')

Just displaying a few red texts requires so much code, and the amount of code required for more and more complex displays can be imagined.

HTML

tkinter display HTML? Well, let me tell you, it's totally fine .

display HTML3

Just through tkhtmlviewthe package. This package implements HTML3 rendering by using Text.

pip install tkhtmlview

The disadvantages are as follows:

  1. HTML3 renders poorly because of the choice to use rich text components for hypertext
  2. The flexibility is not as good as using the code directly

display HTML4

Just through tkinterwebthe package. This package implements HTML4 rendering by customizing a TkHtml component.

pip install tkinterweb

This one seems to render well, but it has its drawbacks:

  1. The web page download is slow because it is limited by the speed of the Python module
  2. The css style of HTML4 appears rough
  3. A large number of callback functions need to be bound, otherwise the effect of rendering HTML4 cannot be achieved

display HTML5

To display HTML5 in tkinter, external support is required.

Third-party packages:

pip install tkinterie //调用系统ieframe
pip install mbpython //使用miniblink.dll,需要额外下载
pip install cefpython //使用谷歌内核

Other methods:

Embeds a small browser window .

Among them, tkinterie depends on the system; mbpython cannot play video; cefpython is bulky; embedded windows cannot effectively control components.

Rich Text Markup Language

In tkinter, rich text rendering is realized by operating rich text markup language.

Markdown

Convert Markdown to HTML through markdown2the package, and then use the method mentioned above to render HTML. There is currently no component or application that directly renders Markdown.

Tin

The Tin markup language was defined and created by me. The Tin markup language uses TinText for rendering, which can be used for rich text display or as "Tinlayout" for page layout.

Tin

Tin knowledge base .

pip install tinengine

Main character TKR (tkinter rich form)

tkr is a rich text format defined based on tkinter.Text itself, but this is not native to tkinter, but I proposed it through the style description of Text.

Format

The version of tkr is: 1. At present, tkr can only describe text content, and content such as bold, italic and tag_bind cannot be rendered, because this involves other libraries in tkinter.

The format of the tkr file is as follows:

=tagon=
tagname1 {'background':'','bd':'0','cursor':'hand2',...}
tagname2 {...}
...
=tagoff=

=texton=
[('tagon','tagname1','1.0'),('text','words use tagname1','1.0'),('tagoff','tagname1','1.17'),...]
=textoff=

Through the format description, it is obvious to see the content in the tkr file. The tag part defines different styles of different tags, and the text part describes how the Text component renders the content.

How to generate & render

This content is not to be discussed in this article, and these two contents are reserved for future articles.

However, I have already figured out how to initially complete the generation and rendering of the tkr file.

Analysis generation

Use Textdumpmethod to get the content description, throughtag_configMethod to get style description.

rendering method

First parse the style description, and then render one by one through the content of the text part.

Why use TKR

The tkr format is a format specified by tkinter itself. It has good coupling with tkinter's Text and has many advantages.

  1. Based on native tkinter, generally do not need the support of other libraries
  2. Taking the description format as the main body, it is convenient for parsing and rendering
  3. Faster than rendering HTML
  4. Compared with Text itself, the code operation is more concise, because the tkr file does not need to be handwritten, and its analysis and generation will be explained in detail in future articles

Of course, tkr also has disadvantages:

  1. Unable to describe dynamic content. Compared with Tin, it lacks dynamic rich text functions and interactive functions, and can only reach the level of Markdown
  2. It is limited to the content that Text can display, and currently cannot embed components
  3. lack of style

Obviously, all the above-mentioned rich text renderings have advantages and disadvantages in tkinter, but the biggest advantage of the tkr format file is that it is quite convenient to use for tkinter .


epilogue

Regarding the TKR format file, I will quote it here first, and other articles in the column will explain the TKR file in detail.

Guess you like

Origin blog.csdn.net/tinga_kilin/article/details/118930384