Home > Software engineering >  Adjust the window size with the text length in it with tkinter
Adjust the window size with the text length in it with tkinter

Time:03-18

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related