Home > Mobile >  Line break widgets at line ending
Line break widgets at line ending

Time:12-18

I want to draw multiple widgets in a row. The problem is, I don't want to scroll horizontally . THe lines of widgets should wrap at the end of the line (like inline elements in HTML/CSS would do).

But how can this be achieved? How to wrap widgets at line ending?

from tkinter import Tk
from tkinter.ttk import Button, Frame

root = Tk()
frame = Frame(root)
frame.pack()
for _ in range(10):
    button = Button(frame)
    button.pack(side='left')
root.mainloop()

enter image description here

CodePudding user response:

One simple solution is to put the widgets in a Text widget with wrapping turned on.

It might look something like this:

from tkinter import Tk, Text
from tkinter.ttk import Button

root = Tk()
container = Text(root, wrap="char")
container.pack(fill="both", expand=True)

for _ in range(10):
    button = Button(container, text=f"Button #{_}")
    container.window_create("end", window=button)
container.configure(state="disabled")
root.mainloop()

screenshot - narrow screenshot - wide

  • Related