Welcome, guest | Sign In | My Account | Store | Cart

The solution consists in putting the Text widget inside a frame, forcing the frame to a fixed size by deactivating size propagation and configuring the Text widget to expand and fill both directions (to stick to the frame borders).

https://stackoverflow.com/questions/14887610/how-to-specify-the-dimensions-of-a-tkinter-text-box-in-pixels

Python, 31 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
try:
    from Tkinter import Frame, Text, Tk
    from Tkconstants import YES, BOTH
except ImportError:
    from tkinter import Frame, Text, Tk
    from tkinter.constants import YES, BOTH

class Text2(Frame):
    def __init__(self, master, width=0, height=0, **kwargs):
        self.width = width
        self.height = height

        Frame.__init__(self, master, width=self.width, height=self.height)
        self.text_widget = Text(self, **kwargs)
        self.text_widget.pack(expand=YES, fill=BOTH)

    def pack(self, *args, **kwargs):
        Frame.pack(self, *args, **kwargs)
        self.pack_propagate(False)

    def grid(self, *args, **kwargs):
        Frame.grid(self, *args, **kwargs)
        self.grid_propagate(False)

root = Tk()

# Now width and height are in pixels. They are not now the number of columns and the number of lines respectively
Text2(root, width=90,height=120).pack()


root.mainloop()
Created by Miguel Martínez López on Fri, 6 Jun 2014 (MIT)
Python recipes (4591)
Miguel Martínez López's recipes (56)

Required Modules

  • (none specified)

Other Information and Tasks