0

I am working on a treeview in which the text in one column (address) can not fit into a single line. My code looks like this:

# Create the invoice window
    invoice = Tk()
    invoice.title("Invoices")
    invoice.geometry("1800x1000")

    # Create treeview frame
    tree_frame = Frame(invoice)
    tree_frame.pack(pady=10)

    # Create scrollbar for treeview
    tree_scroll = Scrollbar(tree_frame)
    tree_scroll.pack(side=RIGHT, fill=Y)

    # Create treeview
    tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set, selectmode="extended")
    tree.pack()

    # Configure scrollbar
    tree_scroll.config(command=tree.yview)

    # Define the columns of the treeview
    tree['columns'] = (
    "Invoice Number", "Company Name", "Company Address", "VAT Number", "Total", "Currency", "Invoice Date")

    # Place the columns
    tree.column("#0", width=0, stretch=NO)
    tree.column("Invoice Number", anchor=CENTER, width=140)
    tree.column("Company Name", anchor=CENTER, width=140)
    tree.column("Company Address", anchor=CENTER, width=140)
    tree.column("VAT Number", anchor=CENTER, width=140)
    tree.column("Total", anchor=CENTER, width=140)
    tree.column("Currency", anchor=CENTER, width=140)
    tree.column("Invoice Date", anchor=CENTER, width=140)

    # Create headings for columns
    tree.heading("#0", text="", anchor=W)
    tree.heading("Invoice Number", text="Invoice Number", anchor=CENTER)
    tree.heading("Company Name", text="Company Name", anchor=CENTER)
    tree.heading("Company Address", text="Company Address", anchor=CENTER)
    tree.heading("VAT Number", text="VAT Number", anchor=CENTER)
    tree.heading("Total", text="Total", anchor=CENTER)
    tree.heading("Currency", text="Currency", anchor=CENTER)
    tree.heading("Invoice Date", text="Invoice Date", anchor=CENTER)

How can I implement a new line so that when the address gets to the end of the space, it continues from the new line? Of course, the whole row should be the same height.

  • Do you want [to wrap text](https://stackoverflow.com/questions/51131812/wrap-text-inside-row-in-tkinter-treeview) in "Company Address"? – 8349697 Jul 21 '21 at 11:55

1 Answers1

0

There's a minwidth option to set minimum width of how much column can contract but I assume the address is too long and you want to wrap text instead:

You will need to import the textwrap module for this:

import textwrap

First, define a function to wrap text:

def wrap(string, length=8):
    if len(companyAddress) >= 10: # this if loop is only for when you don't know the length of the address and want to add newline after it reaches certain length. Otherwise remove the if loop and leave the return statement in.
        
        return '\n'.join(textwrap.wrap(string, length))

And that's it. Now, if you want to insert the data in, just call the function on the value needed to be wrapped.

tree.insert("", "end", iid=0, text="", values=("invoice number", ...(values)... ,wrap("insert company address here")))

just call this function on any other value you want to be wrapped.

Timo
  • 46
  • 7
  • Hey, thank you very much for your answer. Now it wraps the text but the address is cut off when there is another entry below. So is there a way to adjust the row height according to the length of the address, or at least adjust the row height in general? – SarpHarbali Jul 21 '21 at 13:35
  • @SarpHarbali tkinter treeview is designed to have uniformed rows, rows can't have different heights unfortunately. However, you can definitely change the row heights of all the rows using ttk.Style and .configure. `s = ttk.Style() # next line s.configure('Treeview', rowheight=20) # change the value of rowheight to whatever fits` – Timo Jul 22 '21 at 06:05