Python Forum

Full Version: Build a matrix by pressing buttons of an interface in Tkinter which extract data from
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello I would like to ask for help.
From the matrix in excel shown below
A B C D E F G
1 2 1 1 7 1 1
2 3 2 4 4 2 5
3 4 3 4 5 4 6
4 5 5 5 5 6 7
4 2 4 6 5 5 8

I want to extract the numeric values from columns A, C and E to form a matrix like the one shown below.
1 1 7
2 2 4
3 3 5
4 5 5
4 4 5


However, I want to do it, through this interface that I have developed in Tkinter by pressing the buttons A, C and E.
Currently I have programmed in such a way that when pressing the buttons, the columns are extracted from the file in excel, as shown below.

from tkinter import*


ventana=Tk()
ventana.geometry("600x600")
ventana.title("Prueba")


def A():
    import pandas as pd
    datos= pd.read_excel('proyecto matriz.xlsx')
    print(datos.iloc[:,0:1])

def B():
    import pandas as pd
    datos= pd.read_excel('proyecto matriz.xlsx')
    print(datos.iloc[:,1:2])

def C():
    import pandas as pd
    datos= pd.read_excel('proyecto matriz.xlsx')
    print(datos.iloc[:,2:3])

def D():
    import pandas as pd
    datos= pd.read_excel('proyecto matriz.xlsx')
    print(datos.iloc[:,3:4])

def E():
    import pandas as pd
    datos= pd.read_excel('proyecto matriz.xlsx')
    print(datos.iloc[:,4:5])

def F():
    import pandas as pd
    datos= pd.read_excel('proyecto matriz.xlsx')
    print(datos.iloc[:,5:6])

def G():
    import pandas as pd
    datos= pd.read_excel('proyecto matriz.xlsx')
    print(datos.iloc[:,6:7])
    
botonA=Button(text="A",command=A).pack()
botonB=Button(text="B",command=B).pack()
botonC=Button(text="C",command=C).pack()
botonD=Button(text="D",command=D).pack()
botonE=Button(text="E",command=E).pack()
botonF=Button(text="F",command=F).pack()
botonG=Button(text="G",command=G).pack()
However, I want to form a matrix with only the numerical values, how can I do it?
note: I would like to design in such a way as to build the matrix grouping any column of the excel file for example A, B and C or D, F and G
Previously I have managed to form the matrix by extracting the numerical values from columns A, C and E using the following code
import pandas as pd
datos= pd.read_excel('proyecto matriz.xlsx')
df=pd.DataFrame(datos, columns=["A" ,"B","D"])
matriz=df.values.tolist()
print(matriz)
Please wrap code in Python tags so it retains the indentation. Please post all referenced information. Do not provide links to documents.

This code sets "botonA" to the return value of pack() which is None.
botonA=Button(text="A",command=A).pack()
If you want a handle to the button object you need to split the command in two.
botonA=Button(text="A",command=A)
botonA.pack()
You should have only 1 import pandas, and it should be at the top of the module, not embedded in a function. I would also change the code to only read the spreadsheet once and have the functions copy the desired values.

I looked at Help.docx. Why don't you use the method you have already had success with?
df = pd.DataFrame(datos, columns=["A", "B", "D"]
Something like this:
import tkinter as tk
import pandas as pd

datos = pd.DataFrame({
    'A':[1, 2, 3, 4, 4],
    'B':[2, 3, 4, 5, 2],
    'C':[1, 2, 3, 5, 4],
    'D':[1, 4, 4, 5, 6],
    'E':[7, 4, 5, 5, 5],
    'F':[1, 2, 4, 6, 5],
    'G':[1, 5, 6, 7, 8]})

root = tk.Tk()

def print_column(column):
    '''Extract values from column and print them for now'''
    df = pd.DataFrame(datos, columns=[column])
    values = [row[0] for row in df.values]
    print(values)  # Replace with adding to matrix

def button(column):
    '''Make a button that prints the values of a column.  comumn is the column header'''
    button = tk.Button(root, text=column, command=lambda: print_column(column))
    button.pack()
    return button

buttons = [button(column) for column in datos.columns]
root.mainloop()