Pulling excel rows to display as a grid in tkinter

2024/9/20 2:56:01

I am imaging fluorescent cells from a 384-well plate and my software spits out a formatted excel analysis of the data (16 rowsx24 columns of images turns into a list of data, with 2 measurements from each well, ~800 data points). Because there is a lot of manual interaction with the data, I want to automate my work by taking the information that's indexed in the excel sheet and map it as a tkinter grid. I want to take my data, have it formatted from a list back to the original 16x24 display. I want to be able to interact with that data, among other things, but as a newbie in python, I'm having a difficult time mapping this data.

I used pandas to read my dataframe, but can't seem to display my list to the appropriate grid box. Ideally, I'd like the values in the excel list to display on the corresponding cell in the tkinter grid. For example, in my excel list, [1]:https://i.sstatic.net/t0Zrm.jpg, the two data points from each well are listed. I want to take the values from each cell and display them to the corresponding grid in tkinter, so the average of values of "A1" would be mapped onto column 1, row 1 of the grid, A2 would be col 2 row 1, A3 would be col 3 row 1, and so on and so forth.

Any help would be great, thank you.

from tkinter import *import pandas as pdfrom pandas import ExcelWriterfrom pandas import ExcelFile
df = pd.read_excel('/Users/Zammam/PycharmProjects/Filipin_Analysis_Zammam/data.xlsx', sheet_name='Dataps')
print(()
class Cell():FILLED_COLOR_BG = "green"EMPTY_COLOR_BG = "white"FILLED_COLOR_BORDER = "green"EMPTY_COLOR_BORDER = "black"def __init__(self, master, x, y, size):self.master = masterself.abs = xself.ord = yself.size= sizeself.fill= Falsedef _switch(self):""" Switch if the cell is filled or not. """self.fill= not self.filldef draw(self):if self.master != None :fill = Cell.FILLED_COLOR_BGoutline = Cell.FILLED_COLOR_BORDERif not self.fill:fill = Cell.EMPTY_COLOR_BGoutline = Cell.EMPTY_COLOR_BORDERxmin = self.abs * self.sizexmax = xmin + self.sizeymin = self.ord * self.sizeymax = ymin + self.sizeself.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill, outline = outline)class CellGrid(Canvas):def __init__(self,master, rowNumber, columnNumber, cellSize, *args, **kwargs):Canvas.__init__(self, master, width = cellSize * columnNumber , height = cellSize * rowNumber, *args, **kwargs)self.cellSize = cellSizeself.grid = []for row in range(rowNumber):line = []for column in range(columnNumber):line.append(Cell(self, column, row, cellSize))self.grid.append(line)#memorize the cells that have been modified to avoid many switching of state during mouse motion.self.switched = []#bind click actionself.bind("<Button-1>", self.handleMouseClick)  #bind moving while clickingself.bind("<B1-Motion>", self.handleMouseMotion)#bind release button action - clear the memory of midified cells.self.bind("<ButtonRelease-1>", lambda event: self.switched.clear())self.draw()def draw(self):for row in self.grid:for cell in row:cell.draw()def create_text(self):for row in self.grid:for cell in row:for i in df:cell.create_text(text = df)def _eventCoords(self, event):row = int(event.y / self.cellSize)column = int(event.x / self.cellSize)return row, columndef handleMouseClick(self, event):row, column = self._eventCoords(event)cell = self.grid[row][column]cell._switch()cell.draw()#add the cell to the list of cell switched during the clickself.switched.append(cell)def handleMouseMotion(self, event):row, column = self._eventCoords(event)cell = self.grid[row][column]if cell not in self.switched:cell._switch()cell.draw()self.switched.append(cell)if __name__ == "__main__" :app = Tk()grid = CellGrid(app, 16, 24, 15)grid.pack()app.mainloop()
Answer

I'm not familiar with pandas so I will use openpyxl to demonstrate. It will probably be something similar with pandas or even simpler.

from openpyxl import load_workbook
import tkinter as tkroot = tk.Tk()file = "your_excel_file.xlsx"
wb = load_workbook(file, data_only=True)
ws = wb.activer = 0
for row in ws:c = 0for cell in row:tk.Label(root,text=cell.value).grid(row=r,column=c)c+=1r+=1root.mainloop()

A quick look at using pandas instead:

df = pd.read_excel('your_excel_file.xlsx',header=None)for i, row in df.iterrows():c = 0for cell in row:tk.Label(root, text=cell).grid(row=i, column=c)c += 1
https://en.xdnf.cn/q/119361.html

Related Q&A

Django Migrating DB django.db.utils.ProgrammingError: relation django_site does not exist

Doing a site upgrade for Django, now pushing it to the server when I try python manage.py makemigrations I get this error (kpsga) sammy@kpsga:~/webapps/kpsga$ python manage.py makemigrations Traceback …

list intersection algorithm implementation only using python lists (not sets)

Ive been trying to write down a list intersection algorithm in python that takes care of repetitions. Im a newbie to python and programming so forgive me if this sounds inefficient, but I couldnt come …

In keras\tensorflow, How adding CNN layers to last layer of ResNet50V2 that pre-train on imagenet

I am trying to drop the last layer and add a simple CNN instead like the following, model = Sequential() base_model = ResNet50V2(include_top=False, weights="imagenet", input_shape=input_shape…

How to get missing date in columns using python pandas [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

vigenere cipher - not adding correct values

I want to get specific values from a for loop to add to another string to create a vigenere cipher.heres the code.userinput = input(enter message) keyword = input(enter keyword) new = for a in keyword…

Why isnt my output returning as expected?

So I wrote this code def diagsDownRight(M):n = len(M)m = [[] * (n - i - 1) + row + [] * i for i, row in enumerate(M)]return ([.join(col) for col in zip(*m)]), [.join(col[::-1]) for col in zip(*m)] def …

Django Stripe payment does not respond after clicking the Submit Payment button

I have an e-commerce application that Im working on. The app is currently hosted on Heroku free account. At the moment I can select a product, add it on the cart and can get up to the stripe form and t…

get file path using backslash (\) in windows in python [duplicate]

This question already has answers here:How can I put an actual backslash in a string literal (not use it for an escape sequence)?(4 answers)Closed 2 years ago.How to get result exactly the same format…

Printing progress bar on a console without the use of for -loop

I have a script written in python, where I have a statement:Process.open() //some parametersWhich executes a command and puts the output on the console ,where I do not know the time taken to execute t…

ModuleNotFoundError: No module named verovio

Hi there I would like to run my flask app in a container but I got stucked caused of a third party module. (I am using PyCharm)This is my docker file:FROM python:3-alpineMAINTAINER fooCOPY app /appWORK…