How would I specify the dimensions of a Tkinter text box using pixels? I am not trying to change the font size, I am using this to help me scale it to the size of the window.
-
1There is rately ever a time when you need to do this. Do you have some sort of unique constraint, or do you just not understand how to create well behaved layouts? – Bryan Oakley Feb 15 '13 at 19:04
-
70Or perhaps he just wants the question answered. – rhody May 19 '15 at 23:27
-
2rhody, you made my day! rofl – User1493 Mar 29 '19 at 04:53
-
@BryanOakley very useful question, e.g. if you need a widget to show an image, but you don't have yet the image when you create the gui, but do know what size you want to show the image – Roland Jun 24 '23 at 20:55
-
@Roland: Yes, everyone is making good points. The reason I asked is that the answer might depend on exactly what the OP is trying to accomplish. The answer might be to use `place`, or it might be to put the text widget in a frame that can be defined in pixels, or it might include creating a binding so that you can learn the size of the root window after it's been rendered, etc. – Bryan Oakley Jun 24 '23 at 20:58
10 Answers
You can do it by putting it inside a frame, forcing the frame to a fixed size by deactivating size propagation and configuring the entry to stick to the frame borders. Should work in a similar way with pack, too.
import Tkinter # tkinter with small t for python 3
#import ttk # nicer widgets
root = Tkinter.Tk()
mainFrame = Tkinter.Frame(root)
mainFrame.grid()
button = Tkinter.Button(mainFrame, text="dummy")
button.grid()
entryFrame = Tkinter.Frame(mainFrame, width=454, height=20)
entryFrame.grid(row=0, column=1)
# allow the column inside the entryFrame to grow
entryFrame.columnconfigure(0, weight=10)
# By default the frame will shrink to whatever is inside of it and
# ignore width & height. We change that:
entryFrame.grid_propagate(False)
# as far as I know you can not set this for x / y separately so you
# have to choose a proper height for the frame or do something more sophisticated
# input entry
inValue = Tkinter.StringVar()
inValueEntry = Tkinter.Entry(entryFrame, textvariable=inValue)
inValueEntry.grid(sticky="we")
root.mainloop()

- 2,023
- 3
- 21
- 30
I've had the same question, and it seems that the simplest answer is to instantiate a Frame object of a specified size, pack it, and then use the place method to place and size widgets within it. I've found that the invocation of the pack_propagate(0) method on the Frame object is unnecessary.
Here's a short piece of code that exemplifies this method on several widgets.
The last widget instantiated in this script is a text box widget with the size specified in pixels, as per your question.
from Tkinter import * #For python 3 use: from Tkinter import *
root = Tk()
root.title("Fee Fie Foe Fum")
frame=Frame(root, width=300, height=160)
frame.pack()
button1 = Button(frame, text="Mercy!")
button1.place(x=10, y=10, height=30, width=100)
button2 = Button(frame, text="Justice!")
button2.place(x=10, y=50, height=30, width=100)
text1 = Label(text="Verdict:")
text1.place(x=10, y=90)
tbox1 = Text(frame)
tbox1.place(x=10, y=115, height=30, width=200)
root.mainloop()
This does, of course, lock you into the place method for designing your Tkinter UI, but it does allow you to specify the dimensions of many widgets in pixels. Subsequent use of the pack method on child widgets will overwrite your placements and sizing.
Your code will probably be better organized if you express it in an object-oriented fashion, but this example simply shows the sequence of operations necessary to accomplish the sizing of widgets in pixels rather than other units.

- 15
- 3

- 392
- 3
- 9
Do it like this:
from tkinter import *
root = Tk()
t = Text(root)
t.pack(fill=BOTH, expand=1)
root.mainloop()
The fill=BOTH
and the expand=1
are the most important parts. They make it expand with the window, and without one of them the window wouldn't expand a certain direction.

- 77
- 1
- 6
-
How does this **specify dimensions**??? (This answer should be downvoted, only that I don't like doing that) – Apostolos Dec 25 '21 at 07:58
-
-
Here is how I did it. I placed the dimensions of the Text object inside the place tag
firstname = Text()
firstname.place(x = 170, y = 20, height = 25, width = 200)

- 778
- 12
- 16
-
It is very important to understand that when you create the text widget, any values you specify, is relative to one character. So height of 1, is 1 character high. Because of this, many may find it useful to use place to specify width, and specify the height in the creation section, to prevent having boxes with half the text on the next line. – Jan 28 '22 at 01:25
You can't set the width
option of a Text to anything but an integer representing the number of characters. But when you know your font, you can calculate the width of a string in that font. In the reverse, if you know how many pixels wide you want your Text to be, you can calculate how many characters would fit into that pixel length and set the Text width to that number of characters (roughly). It's simple with a fixed-width font but gets trickier with a variable-width font.
The default font for Text widgets is 'TkFixedFont'.
import tkinter as tk
tk.font.Font(font='TkFixedFont').actual()
gives:
{'family': 'Courier New', 'size': 10, 'weight': 'normal', 'slant': 'roman', 'underline': 0, 'overstrike': 0}
It may not be the perfect answer to the question, but it allows you to get something close without having to introduce additional widgets. If you want something more precise, a Frame wrapper may be the way to go.

- 5,350
- 1
- 24
- 32
This helped me a lot:
You can also use the height and width options to explicitly set the size. If you display text in the button, these options define the size of the button in text units. If you display bitmaps or images instead, they define the size in pixels (or other screen units). You can specify the size in pixels even for text buttons, but that requires some magic. Here’s one way to do it (there are others):
f = Frame(master, height=32, width=32) f.pack_propagate(0) # don't shrink f.pack()
b = Button(f, text="Sure!") b.pack(fill=BOTH, expand=1)

- 2,380
- 3
- 18
- 31
-
I don't get what the solution is here. The question is about a text box? – Denis G. Labrecque Mar 19 '21 at 01:45
-
I would rather use inter division as per the font I use:
text_width = 300
text_height = 200
text_widget = tk.Text(root, width=text_width // 6, height=text_height // 14)
text_widget.pack()
6 and 14 are approximate and need to be adjusted as per the font we use.

- 326
- 3
- 17
This is the way I often do this:
from tkinter import *
class App:
def __init__(self):
self.tk = Tk()
self.canvas = Canvas(self.tk, width = 500, height = 500)
self.canvas.pack()
This sets the dimensions to 500 pixels in each direction. When you create an instance of this class, it displays a window of this size. Hope this helps!

- 6,537
- 2
- 37
- 69
-
9This is a `Canvas` -- `Text` box Widgets accept size in units of characters I believe which makes it not quite so easy if you want pixels... – mgilson Feb 15 '13 at 03:11
-
1Have a look at the reference site for the tcl Tk (for version 8.6) itself [here](https://www.tcl.tk/man/tcl8.6/TkCmd/text.htm#M21). It says, that `width` is the amount of characters per line and `height` is the amount of lines (roughly speaking). See also comment from mgilson. – PiMathCLanguage Apr 28 '20 at 08:37
The height can be set using configure
var = tk.Text(self)
var.configure(height=1)
entry_2 = Entry(win, justify = 'center')
entry_2.grid(row =2,column = 7,ipadx=10, ipady=10)

- 4,468
- 4
- 28
- 51

- 3
- 2