Specify the dimensions of a Tkinter text box in pixels

TkinterPythonGUI-Programming

You can set the position of a text widget by specifying its dimension using place(**option) geometry manager. Instantiating a widget inside a frame makes the widget independent throughout the application window. We then use the place()geometry manager to assign the width and height of the widget inside the window. The pixels justify how accurately a widget is positioned in the window. Thus, the place() geometry manager provides a grid system where we can place any widget in a particular location.

Example

# Import required libraries
from tkinter import *
from tkinter import ttk
from lorem_text import lorem

# Create an instance of tkinter frame
win= Tk()

# Set the window size
win.geometry("700x350")

# Add a Text widget and insert some dummy text
text= Text(win, wrap= WORD, font= ('Courier 15 bold'))
text.insert(END,lorem.sentence())
text.place(x=10, y= 10, width= 400, height= 300)

win.mainloop()

Output

When we run the above code, a Text widget will appear in the window with some dummy text. The dimension of the Text widget can be updated by changing the values of x, y, width, and height in place() geometry manager.

raja
Published on 07-Jun-2021 11:29:18
Advertisements