alex
alex

Reputation: 83

Adjust the window size with the text length in it with tkinter

Do you know if it's possible to adjust automatically the width of the window to match the length of the text in it? For example the picture 1 fits well, but in the picture 2 the text get out of the window.

Or if better, is it possible to do line break to fit the text in the window?

PS: to insert the text in my window I am using a ttk.Label()

Upvotes: 1

Views: 1626

Answers (2)

Tharusha Jayasooriya
Tharusha Jayasooriya

Reputation: 177

if you dont want to explicitly set the size of the window to a specific size then you can do

window.geometry("")

and it will automatically resize to fit the content of the window

Upvotes: 3

scotty3785
scotty3785

Reputation: 7006

Try using the wraplength attribute of the label

import tkinter as tk

root = tk.Tk()
tk.Label(root,width=40,text="This is going to be far too long to fit on the screen that I have chosen").grid()
tk.Label(root,width=40,wraplength=100,text="This is going to be far too long to fit on the screen that I have chosen but will wrap").grid()

root.mainloop()

The behaviour seems a bit inconsistent to me but it should wrap at 40 characters.

Upvotes: 1

Related Questions