Python Forum
Build a matrix by pressing buttons of an interface in Tkinter which extract data from
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Build a matrix by pressing buttons of an interface in Tkinter which extract data from
#1
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)
Larz60+ write Sep-13-2021, 10:27 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.

Attached Files

.docx   Help.docx (Size: 130.8 KB / Downloads: 199)
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,058 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Why can't it extract the data from .txt well? Melcu54 3 679 Aug-20-2023, 10:07 PM
Last Post: deanhystad
  Check if two matrix are equal and of not add the matrix to the list quest 3 825 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  python Extract sql data by combining below code. mg24 1 965 Oct-03-2022, 10:25 AM
Last Post: mg24
  SQL Alchemy help to extract sql data into csv files mg24 1 1,783 Sep-30-2022, 04:43 PM
Last Post: Larz60+
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,281 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Python Pandas: How do I extract all the >1000 data from a certain column? JaneTan 0 1,563 Jul-17-2021, 09:09 AM
Last Post: JaneTan
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,370 May-03-2021, 06:30 AM
Last Post: Gribouillis
  Need help on extract dynamic table data Dr_Strange 0 2,493 Apr-30-2021, 07:03 AM
Last Post: Dr_Strange
  Python modules to extract data from a graph? bigmit37 5 22,417 Apr-09-2021, 02:15 PM
Last Post: TysonL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020