简体   繁体   中英

How do I double space a tkinter text widget without using extra newline characters?

I have a tkinter Text widget in which I want to display the contents of a dict as a reference. I have no trouble configuring the style of each item, but I'm not pleased with the display because the lines are too close together for me to read comfortably. I want to "double space" the list, but I don't want to add extra newline characters, as this would get confusing and could potentially screw up how tagged areas are displayed. Specifically, I want to space the lines out so that the following criteria are met:

  1. no vertical space above the first line of the listing
  2. no vertical space below the last line of the listing
  3. a consistent vertical spacing I specify between the lines of the listing
  4. the text widget is wide enough to contain all the text without wrapping
  5. each line of the listing is separated from the next by exactly one newline character
  6. each key and each value is separately tagged and configurable

Here's my current code:

import tkinter

dictionary = {
  "test": "blue",
  "test2": "red",
  "test3": "green"
}
spacing = 30

root = tkinter.Tk()
text_widget = tkinter.Text(root)
text_widget.pack()
# "works" with this line, but don't want the side effect:
# text_widget.configure(width=5)

for index, (key, value) in enumerate(dictionary.items()):
  text_widget.mark_set("begin", "insert")
  text_widget.insert("insert", key+": ", (key,))
  text_widget.insert("insert", value+"\n", (value,))
  text_widget.tag_configure(key, font="courier 12 normal")
  text_widget.tag_configure(value, foreground=value)

#corrected the starting index as per the comments
text_widget.tag_add("whole", "1.0", "end")
text_widget.tag_configure("whole", spacing2=spacing)

root.mainloop()

Though there are three spacing tag attributes that look like one of them should work for my case, none of them do. The spacing2 attribute comes frustratingly close, but according to https://www.tutorialspoint.com/python/tk_text.htm ,

spacing2

This option specifies how much extra vertical space to add between displayed lines of text when a logical line wraps. Default is 0.

and this functionality doesn't seem particularly useful either, in my opinion.

Since I want to space all the text in the widget, it doesn't matter whether the solution is implemented at the tag level or widget-wide.

How can I put space between the lines of my listing according to the criteria given?

My expected output looks like this:

输出

Python 3.8.2 Tkinter 8.6

Yyou need to use either spacing1 on all but the first line, or spacing3 on all but the last line. spacing1 defines how much space to add above each line, and spacing3 defines how much space to add below each line.

For example, this uses spacing1 on all but the first line:

text_widget.tag_add("whole", "2.0", "end-1c")
text_widget.tag_configure("whole", spacing1=spacing)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM