Home > Net >  Tkinter : How to add multiple columns in a treeview and avoid typeError
Tkinter : How to add multiple columns in a treeview and avoid typeError

Time:04-20

I am trying to create a Treeview with a lot of columns.

Here is my class that create the window with the treeview :

#coding:utf-8
import tkinter as tk
from tkinter import Tk, ttk

class Tableau(Tk):
    def __init__(self):
        super().__init__()

        self.title("My treeview")

        app_width = 1050
        app_height = 600

        screnn_width = self.winfo_screenwidth()
        screnn_heigth = self.winfo_screenheight()

        x = (screnn_width / 2) - (app_width / 2)
        y = (screnn_heigth / 2) - (app_height / 2)

        self.geometry(f'{app_width}x{app_height} {int(x)} {int(y)}')
       #-------------------------------------------Configurer la page--------------------------------------------
        self.config(background="azure")
        self.resizable(0,0)
        self.focus_force()


        #Ajouter un style
        self.style = ttk.Style()

        #Choisir un theme
        self.style.theme_use('default')

        #Configure treeview
        self.style.configure("Treeview",
            background="#D3D3D3",
            foreground="black",
            rowheight=25,
            fieldbackground="#D3D3D3")
        
        #Changer couleur
        self.style.map('Treeview',
            background=[('selected', '#347083')])

        self.frame = tk.Frame(self)
        self.frame.pack(pady=10)

        self.tree_scroll_horizontal = tk.Scrollbar(self.frame, orient='horizontal')
        self.tree_scroll_horizontal.pack(side='bottom', fill='x'  )
        
        self.tree = ttk.Treeview(self.frame,  xscrollcommand=self.tree_scroll_horizontal.set, columns=())
        self.tree.pack()

        self.tree_scroll_horizontal.config(command=self.tree.xview)

        for self.k in range(50):
            self.param = self.tree["columns"]   (str(self.k),)
            self.tree.configure(columns=self.param)
            print(self.k)

    
        for self.j in range(50):
            self.tree.column(column=(str(self.k)), width=500, stretch=tk.NO)
            self.tree.heading(column= (str(self.k)), text=(str(self.k)), anchor=tk.W)

        self.tree.tag_configure('oddrow', background="white")
        self.tree.tag_configure('evenrow', background="lightblue")


if __name__ == "__main__":
    app = Tableau()
    app.mainloop()

I have this error when I run it : TypeError: can only concatenate str (not "tuple") to str

I know that there is a problem with my type, and the error say that this error happen here : line 57, in init self.param = self.tree["columns"] (str(self.k),)

To avoid this error i tried to add the str notation but that didnt worked.

CodePudding user response:

Since you create self.tree with empty tuple as the columns option, the return value of self.tree["columns"] is an empty string instead of empty tuple. You can verify it by printing the type of self.tree["columns"] after the creation of self.tree. That's why exception is raised on the mentioned line.

Actually you can simplify the following for loop:

for self.k in range(50):
    self.param = self.tree["columns"]   (str(self.k),)
    self.tree.configure(columns=self.param)
    print(self.k)

by

self.tree["columns"] = list(range(50))

Also I think that the for self.j in range(50) in the following for loop

for self.j in range(50):
    self.tree.column(column=(str(self.k)), width=500, stretch=tk.NO)
    self.tree.heading(column= (str(self.k)), text=(str(self.k)), anchor=tk.W)

should be for self.k in range(50) instead.

  • Related