拉取 excel 行以在 tkinter 中显示为网格 [英] Pulling excel rows to display as a grid in tkinter

查看:27
本文介绍了拉取 excel 行以在 tkinter 中显示为网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对 384 孔板中的荧光细胞进行成像,我的软件对数据进行了格式化的 excel 分析(16 行 x 24 列的图像变成了数据列表,每个孔有 2 个测量值,约 800 个数据点).因为有很多与数据的手动交互,我想通过获取在 Excel 工作表中索引的信息并将其映射为 tkinter 网格来自动化我的工作.我想把我的数据从列表中格式化回原来的 16x24 显示.我希望能够与这些数据进行交互等,但作为 Python 的新手,我很难映射这些数据.

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.

我使用 Pandas 来读取我的数据框,但似乎无法将我的列表显示到适当的网格框.理想情况下,我希望 excel 列表中的值显示在 tkinter 网格中的相应单元格上.例如,在我的excel列表中,[1]:https://imgur.com/a/N8HAtYl,列出了每个孔的两个数据点.我想从每个单元格中获取值并将它们显示到 tkinter 中的相应网格,因此A1"的平均值将映射到网格的第 1 列第 1 行,A2 将是第 2 行第 1 行,A3将是第 3 行第 1 行,依此类推.

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://imgur.com/a/N8HAtYl, 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 pd

from pandas import ExcelWriter

from 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 = master
        self.abs = x
        self.ord = y
        self.size= size
        self.fill= False

    def _switch(self):
        """ Switch if the cell is filled or not. """
        self.fill= not self.fill

    def draw(self):

        if self.master != None :
            fill = Cell.FILLED_COLOR_BG
            outline = Cell.FILLED_COLOR_BORDER

            if not self.fill:
                fill = Cell.EMPTY_COLOR_BG
                outline = Cell.EMPTY_COLOR_BORDER

            xmin = self.abs * self.size
            xmax = xmin + self.size
            ymin = self.ord * self.size
            ymax = ymin + self.size

            self.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 = cellSize

        self.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 action
        self.bind("<Button-1>", self.handleMouseClick)  
        #bind moving while clicking
        self.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, column

    def 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 click
        self.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()

推荐答案

我对 pandas 不熟悉,所以我将使用 openpyxl 来演示.它可能与熊猫类似,甚至更简单.

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 tk

root = tk.Tk()

file = "your_excel_file.xlsx"
wb = load_workbook(file, data_only=True)
ws = wb.active

r = 0
for row in ws:
    c = 0
    for cell in row:
        tk.Label(root,text=cell.value).grid(row=r,column=c)
        c+=1
    r+=1

root.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 = 0
    for cell in row:
        tk.Label(root, text=cell).grid(row=i, column=c)
        c += 1

这篇关于拉取 excel 行以在 tkinter 中显示为网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆