2

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:

enter image description here

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.

Chopin
  • 113
  • 1
  • 10

4 Answers4

3

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:

enter image description here

acw1668
  • 40,144
  • 5
  • 22
  • 34
0

Try putting the text box into it's own frame.

Some thing like this:

from Tkinter import *
root = Tk()

labelframe = LabelFrame(root, text="LabelFrame")
labelframe.pack()

text = Label(labelframe, text="Text inside labelframe")
text.pack()

root.mainloop()
Programmer
  • 61
  • 5
  • I still can't understand how to add the text with a rectangle to the text box. – Chopin Feb 25 '22 at 21:29
  • The frame willact as a rectangle surrounding the text – Programmer Feb 25 '22 at 21:50
  • Can you please show me how you do it with the Text box? because I really cannot see how to do it from what you wrote. – Chopin Feb 25 '22 at 22:11
  • The code I supplied tell you how to creat a frame and how to put their inside of it. If you would like you can read this article on frames. https://www.tutorialspoint.com/python/tk_frame.htm – Programmer Feb 25 '22 at 23:27
0

You can add the border to the Entry using relief = "solid", centre the text with outline and you can use grid to align the widgets the way you want.

import tkinter as tk

root = tk.Tk()
root.geometry("400x200")
root.grid_columnconfigure(0, weight = 1)

ent1 = tk.Entry(root, relief = "solid", justify = "center")
ent1.insert(0, "hello")
ent1.grid(row = 0, column = 0, pady = 10)

ent2 = tk.Entry(root, relief = "solid", justify = "center")
ent2.insert(0, ".......")
ent2.grid(row = 1, column = 0, pady = 10)

lab1 = tk.Label(root, text = "hello")
lab1.grid(row = 2, column = 0, sticky = "w")

lab2 = tk.Label(root, text = "hello")
lab2.grid(row = 3, column = 0, sticky = "w")

root.mainloop()

Most of this is straightforward, the root.grid_columnconfigure line makes the grid take up the full width of the root window by giving the first column a weight of 1. The result is very similar to your example:
Image of Tkinter window

Henry
  • 3,472
  • 2
  • 12
  • 36
  • But the whole point is to combine it with the text box's texts – Chopin Feb 26 '22 at 09:49
  • maybe is there a way to add a tag configuration to the text box, so that the text will be surrounded by a rectangle? – Chopin Feb 26 '22 at 11:30
  • What do you mean by a text box? An Entry or a Label? If you just want to put everything in a Frame (a box) with a border that's possible. – Henry Feb 26 '22 at 13:41
  • neither of them, I mean Text, is a block of text that can add text to it. – Chopin Feb 26 '22 at 14:28
  • https://www.tutorialspoint.com/python/tk_text.htm see this... I was able to insert the text at the center with JUSTIFY and a tag_configure, but cannot insert a rectangle around the text I am inserting. If it isn't clear in the post I can add to it. – Chopin Feb 26 '22 at 14:31
0

You could create an Entry widget in the textbox using text.window_create(). You could customize the border of the Entry widget, and you would be able to type text inside it. To make it look more like part of the textbox, you should register events so when the user presses Right and the caret is one character left of the Entry, give the Entry focus using focus_set. You could do the same with the Left.

Ethan Chan
  • 153
  • 11