Reputation: 416
The text in a label in tkinter can be wrapped into multiple lines when exceeding the limit given in the parameter wraplength
.
However, this is a number of pixels, but instead, I'd like to use the full window width for this, and the wrap lenght should change whenever the user is changing the window size.
One approach could be to update the parameter manually with something like this:
def update_wraplength(id, root):
id.configure(wraplength=root.winfo_width())
root.after(10, lambda: update_wraplength(id,root))
Is there another way of doing this, maybe a parameter I do not know about?
Upvotes: 3
Views: 4170
Reputation: 424
To enhance the performance of @rmb’s WrappingLabel
within a customtkinter
grid
layout, I created a variation that introduces a resize throttle, which helps reduce the number of updates during resizing.
Sharing it here for reference:
class CtkGridWrappingLabel(ctk.CTkLabel):
def __init__(self, master=None, height=0, width=0, justify=ctk.LEFT, **kwargs):
super().__init__(master, height=height, width=width, justify=justify, **kwargs)
self._last_wrap_length: int = 0
self._resize_after_id = None
self.bind('<Configure>', self._on_resize)
def _on_resize(self, event):
new_wrap_length = int(self.winfo_width() / ctk.ScalingTracker.get_widget_scaling(self))
if new_wrap_length == self._last_wrap_length:
return
self._last_wrap_length = new_wrap_length
# Cancel previous
if self._resize_after_id is not None:
self.after_cancel(self._resize_after_id)
self._resize_after_id = self.after(100, self._update_wrap_length)
def _update_wrap_length(self):
self.configure(wraplength=self._last_wrap_length)
self._resize_after_id = None
Upvotes: 0
Reputation: 573
You would have to update the wraplength every time the window size changes. You can detect when the window size changes with the "<Configure>"
event.
my_label.bind('<Configure>', update_wraplength)
Remember it only works if you have the Label set up to expand to all available space.
Lets see if you can make sense of this code:
import Tkinter as tk
class WrappingLabel(tk.Label):
'''a type of Label that automatically adjusts the wrap to the size'''
def __init__(self, master=None, **kwargs):
tk.Label.__init__(self, master, **kwargs)
self.bind('<Configure>', lambda e: self.config(wraplength=self.winfo_width()))
def main():
root = tk.Tk()
root.geometry('200x200')
win = WrappingLabel(root, text="As in, you have a line of text in a Tkinter window, a Label. As the user drags the window narrower, the text remains unchanged until the window width means that the text gets cut off, at which point the text should wrap.")
win.pack(expand=True, fill=tk.X)
root.mainloop()
if __name__ == '__main__':
main()
Upvotes: 12