Specify the dimensions of a Tkinter text box in pixels

Each Answer to this Q is separated by one/two green lines.

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.

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()

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.

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.

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)

Source: http://effbot.org/tkinterbook/button.htm

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)

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.

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!

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)


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .