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.
CodePudding user response:
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()
CodePudding user response:
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: