I want to have a rectangle border around certain text that is added to the text box from the end and will be placed at the center.
For example:
Unfortunately, I can't find a way to do that, because I don't know how to place texts at the center of the line in the text box, and don't know how to surround a text with a rectangle.
You can wrap a Label
with border between a space and newline with justify='center'
in a Text
widget.
Below is an example:
import tkinter as tk
root = tk.Tk()
textbox = tk.Text(root, width=30, height=10)
textbox.pack()
textbox.tag_config('center', justify='center')
def center_label(textbox, **kwargs):
textbox.insert('end', ' ', 'center')
lbl = tk.Label(textbox, bd=3, relief='solid', **kwargs)
textbox.window_create('end', window=lbl)
textbox.insert('end', '\n\n')
center_label(textbox, text='hello', width=10, font='Arial 12 bold')
center_label(textbox, text='............', width=20)
textbox.insert('end', '\nhello\n')
root.mainloop()
Result: