Skip to main content
Formato de código
Origen Enlace
FJSevilla
  • 62.1k
  • 7
  • 43
  • 68
import numpy as np
import tkinter as tk
from tkinter import ttk 


resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
 

root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)

# ============================================================================== #
##############################################################################
tree = ttk.Treeview(frame_labelr)
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {iindex}" for iindex in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index}", anchor=tk.CENTER, width=40, stretch=tk.NO)
    tree.heading(f"#{index}", text=column)

# Insertar datos
for iindex, row in enumerate(resultado):
    tree.insert("", tk.END, text=itext=index,  values=(*row,))

# ============================================================================== #

root.mainloop()
 

O tambiénNota: el código que te interesa es el encerrado entre las lineas # ===...=== #, el resto es solo para hacer el ejemplo fácilmente reproducible.


También puedes ocultarla mediante el argumento/atributo show de TreeView y proceder como hacías antes:

import numpy as np
import tkinter as tk
from tkinter import ttk 


resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
 

root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)

# ============================================================================== #
##############################################################################
tree = ttk.Treeview(frame_labelr, show="headings")
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {iindex}" for iindex in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index + 1}", anchor=tk.CENTER, width=40)
    tree.heading(f"#{index + 1}", text=column)

# Insertar datos
for iindex, row in enumerate(resultado):
    tree.insert("", tk.END, values=(iindex, *row))

# ============================================================================== #

root.mainloop()
import numpy as np
import tkinter as tk
from tkinter import ttk

resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
 

root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)


##############################################################################
tree = ttk.Treeview(frame_labelr)
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {i}" for i in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index}", anchor=tk.CENTER, width=40, stretch=tk.NO)
    tree.heading(f"#{index}", text=column)

# Insertar datos
for i, row in enumerate(resultado):
    tree.insert("", tk.END, text=i,  values=(*row,))

root.mainloop()

O también puedes ocultarla mediante el argumento/atributo show de TreeView y proceder como hacías antes:

import numpy as np
import tkinter as tk
from tkinter import ttk

resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
 

root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)


##############################################################################
tree = ttk.Treeview(frame_labelr, show="headings")
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {i}" for i in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index + 1}", anchor=tk.CENTER, width=40)
    tree.heading(f"#{index + 1}", text=column)

# Insertar datos
for i, row in enumerate(resultado):
    tree.insert("", tk.END, values=(i, *row))

root.mainloop()
import numpy as np
import tkinter as tk
from tkinter import ttk 


resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)

# ============================================================================== #

tree = ttk.Treeview(frame_labelr)
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {index}" for index in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index}", anchor=tk.CENTER, width=40, stretch=tk.NO)
    tree.heading(f"#{index}", text=column)

# Insertar datos
for index, row in enumerate(resultado):
    tree.insert("", tk.END, text=index,  values=(*row,))

# ============================================================================== #

root.mainloop()
 

Nota: el código que te interesa es el encerrado entre las lineas # ===...=== #, el resto es solo para hacer el ejemplo fácilmente reproducible.


También puedes ocultarla mediante el argumento/atributo show de TreeView y proceder como hacías antes:

import numpy as np
import tkinter as tk
from tkinter import ttk 


resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)

# ============================================================================== #

tree = ttk.Treeview(frame_labelr, show="headings")
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {index}" for index in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index + 1}", anchor=tk.CENTER, width=40)
    tree.heading(f"#{index + 1}", text=column)

# Insertar datos
for index, row in enumerate(resultado):
    tree.insert("", tk.END, values=(index, *row))

# ============================================================================== #

root.mainloop()
se eliminaron 2 caracteres en el cuerpo
Origen Enlace
FJSevilla
  • 62.1k
  • 7
  • 43
  • 68
import numpy as np
import tkinter as tk
from tkinter import ttk

resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])


root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)


##############################################################################
tree = ttk.Treeview(frame_labelr)
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {i}" for i in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index}", anchor=tk.CENTER, width=40, stretch=tk.NO)
    tree.heading(f"#{index}", text=column)

# Insertar datos
for i, row in enumerate(resultado):
    tree.insert("", tk.END, text=i,  values=listvalues=(row*row,))

root.mainloop()
import numpy as np
import tkinter as tk
from tkinter import ttk

resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])


root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)


##############################################################################
tree = ttk.Treeview(frame_labelr)
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {i}" for i in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index}", anchor=tk.CENTER, width=40, stretch=tk.NO)
    tree.heading(f"#{index}", text=column)

# Insertar datos
for i, row in enumerate(resultado):
    tree.insert("", tk.END, text=i,  values=list(row))

root.mainloop()
import numpy as np
import tkinter as tk
from tkinter import ttk

resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])


root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)


##############################################################################
tree = ttk.Treeview(frame_labelr)
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {i}" for i in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index}", anchor=tk.CENTER, width=40, stretch=tk.NO)
    tree.heading(f"#{index}", text=column)

# Insertar datos
for i, row in enumerate(resultado):
    tree.insert("", tk.END, text=i,  values=(*row,))

root.mainloop()
Origen Enlace
FJSevilla
  • 62.1k
  • 7
  • 43
  • 68

La primera columna es el identificador del item, debes asignarle el valor usando el argumento text, no a través de values:

import numpy as np
import tkinter as tk
from tkinter import ttk

resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])


root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)


##############################################################################
tree = ttk.Treeview(frame_labelr)
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {i}" for i in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index}", anchor=tk.CENTER, width=40, stretch=tk.NO)
    tree.heading(f"#{index}", text=column)

# Insertar datos
for i, row in enumerate(resultado):
    tree.insert("", tk.END, text=i,  values=list(row))

root.mainloop()

O también puedes ocultarla mediante el argumento/atributo show de TreeView y proceder como hacías antes:

import numpy as np
import tkinter as tk
from tkinter import ttk

resultado = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])


root = tk.Tk()
frame_labelr = ttk.Frame(root)
frame_labelr.pack(fill=tk.BOTH, expand=True)


##############################################################################
tree = ttk.Treeview(frame_labelr, show="headings")
tree.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)

scrollbar = ttk.Scrollbar(frame_labelr, orient=tk.VERTICAL, command=tree.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

tree.configure(yscrollcommand=scrollbar.set)

n_filas, n_columnas = resultado.shape
columns = ["Filas"] + [f"Col {i}" for i in range(1, n_columnas + 1)]

# Asignar las columnas al Treeview
tree["columns"] = columns

# Formatear columnas
for index, column in enumerate(columns):
    tree.column(f"#{index + 1}", anchor=tk.CENTER, width=40)
    tree.heading(f"#{index + 1}", text=column)

# Insertar datos
for i, row in enumerate(resultado):
    tree.insert("", tk.END, values=(i, *row))

root.mainloop()

En cualquier caso obtendrás algo como:

introducir la descripción de la imagen aquí