Hi a trying to make a Treeview with differents columns. Seven of those columns are defined, and all of the others are created with a loop. I know how to create columns with a loop, and i know how to create columns manualy, but i dont know how to do it on the same Treeview at the same time.
Here is my code :
# 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(8, 50):
print(self.k)
self.tree["columns"] = ("ID", "Programme", "Date de début", "Date de fin","Effectif nécessaire", "Numéro OP", "Type Hélicoptère", list(range(8, 50)))
self.tree.column(column="#0", width=0, stretch="no")
self.tree.column(column="ID", anchor="center", width=140)
self.tree.column(column="Programme", anchor="center", width=140)
self.tree.column(column="Date de début", anchor="center", width=140)
self.tree.column(column="Date de fin", anchor="center", width=140)
self.tree.column(column="Effectif nécessaire", anchor="center", width=150)
self.tree.column(column="Numéro OP", anchor="center", width=150)
self.tree.column(column="Type Hélicoptère", anchor="center", width=150)
self.tree.column(column=(str(self.k)), width=150, stretch=tk.NO)
self.tree.heading(column="#0", text="", anchor="w")
self.tree.heading(column="ID", text="ID", anchor="center")
self.tree.heading(column="Programme", text="Programme", anchor="center")
self.tree.heading(column="Date de début", text="Date de début", anchor="center")
self.tree.heading(column="Date de fin", text="Date de fin", anchor="center")
self.tree.heading(column="Effectif nécessaire",text="Effectif nécessaire", anchor="center")
self.tree.heading(column="Numéro OP", text="Numéro OP", anchor="center")
self.tree.heading(column="Type Hélicoptère",text="Type Hélicoptère", anchor="center")
self.tree.heading(column= (str(self.k)),text=("Semaine " 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 got this error : _tkinter.TclError: Column index 8 out of bounds
That concern this line : line 68, in init self.tree.column(column=(str(self.k)), width=150, stretch=tk.NO)
So how to create those columns at the same time, on the same treeview, without the same way to do this.
CodePudding user response:
Note that the following line:
self.tree["columns"] = ("ID", "Programme", "Date de début", "Date de fin","Effectif nécessaire", "Numéro OP", "Type Hélicoptère", list(range(8, 50)))
will assign a tuple of 8 items (the last item is the list) to self.tree["columns"]
. So the index are 0 to 7, and exception raised when you try to access item in index 8.
The correct syntax is:
self.tree["columns"] = ["ID", "Programme", "Date de début", "Date de fin", "Effectif nécessaire", "Numéro OP", "Type Hélicoptère"] list(range(8, 50))
And this line should be put before the for loop (including some other lines):
self.tree["columns"] = ["ID", "Programme", "Date de début", "Date de fin", "Effectif nécessaire", "Numéro OP", "Type Hélicoptère"] list(range(8, 50))
self.tree.column(column="#0", width=0, stretch="no")
self.tree.column(column="ID", anchor="center", width=140)
self.tree.column(column="Programme", anchor="center", width=140)
self.tree.column(column="Date de début", anchor="center", width=140)
self.tree.column(column="Date de fin", anchor="center", width=140)
self.tree.column(column="Effectif nécessaire", anchor="center", width=150)
self.tree.column(column="Numéro OP", anchor="center", width=150)
self.tree.column(column="Type Hélicoptère", anchor="center", width=150)
self.tree.heading(column="#0", text="", anchor="w")
self.tree.heading(column="ID", text="ID", anchor="center")
self.tree.heading(column="Programme", text="Programme", anchor="center")
self.tree.heading(column="Date de début", text="Date de début", anchor="center")
self.tree.heading(column="Date de fin", text="Date de fin", anchor="center")
self.tree.heading(column="Effectif nécessaire",text="Effectif nécessaire", anchor="center")
self.tree.heading(column="Numéro OP", text="Numéro OP", anchor="center")
self.tree.heading(column="Type Hélicoptère",text="Type Hélicoptère", anchor="center")
for self.k in range(8, 50):
print(self.k)
self.tree.column(column=str(self.k), width=150, stretch=tk.NO)
self.tree.heading(column=str(self.k), text="Semaine " str(self.k), anchor=tk.W)
CodePudding user response:
Here this should work:
btw there is no need in a for loop to use a object attribute as a placeholder for cantaining the variable of the iterable, in this case range()
and there was no need to in every loop to create all the hardset columns. and you shouldnt do this things in the init function, its just a assembler.
i added two for loops, the first one appended to the column name list i seperated, the number of the range(8, 50) function.
the second for loop is doing the intended work of creating the column and this header.
Solution:
# 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)
column_names = ["ID", "Programme", "Date de début", "Date de fin","Effectif nécessaire", "Numéro OP", "Type Hélicoptère"]
for c in range(8, 50):
column_names.append(str(c))
print(column_names)
self.tree["columns"] = column_names
self.tree.column(column="#0", width=0, stretch="no")
self.tree.column(column="ID", anchor="center", width=140)
self.tree.column(column="Programme", anchor="center", width=140)
self.tree.column(column="Date de début", anchor="center", width=140)
self.tree.column(column="Date de fin", anchor="center", width=140)
self.tree.column(column="Effectif nécessaire", anchor="center", width=150)
self.tree.column(column="Numéro OP", anchor="center", width=150)
self.tree.column(column="Type Hélicoptère", anchor="center", width=150)
self.tree.heading(column="#0", text="", anchor="w")
self.tree.heading(column="ID", text="ID", anchor="center")
self.tree.heading(column="Programme", text="Programme", anchor="center")
self.tree.heading(column="Date de début", text="Date de début", anchor="center")
self.tree.heading(column="Date de fin", text="Date de fin", anchor="center")
self.tree.heading(column="Effectif nécessaire",text="Effectif nécessaire", anchor="center")
self.tree.heading(column="Numéro OP", text="Numéro OP", anchor="center")
self.tree.heading(column="Type Hélicoptère",text="Type Hélicoptère", anchor="center")
for k in range(8, 50):
self.tree.column(column=(str(k)), width=150, stretch=tk.NO)
self.tree.heading(column= (str(k)),text=("Semaine " str(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()