Home > Back-end >  How to make a Tkinter Text Box have a height of 2 but center the text vertically?
How to make a Tkinter Text Box have a height of 2 but center the text vertically?

Time:11-11

I want to have my tkinter textbox have a height of 2, but center the text vertically. I have tried adding a tag_configuration, but it does not seem to have worked. I have been searching on this site and only found ways to center horizontally, not vertically. Any help is appreciated.

        entry = tk.Text(self.frame, height=2, width=20, font=('Corsiva Hebrew', '20'), borderwidth=3,
                             relief='solid')
        entry.tag_configure("center", justify='center')

CodePudding user response:

You can set the spacing1 and spacing3 options to some value (in pixels) and set height=1 instead.

Below is an example:

entry = tk.Text(root, height=1, width=20, spacing1=20, spacing3=20,
                font=('Corsiva Hebrew', '20'), bd=3, relief='solid')
entry.tag_configure('center', justify='center')
entry.insert('end', 'Hello World', 'center')

And output:

enter image description here

  • Related