拉出Excel行以在tkinter中显示为网格

时间:2019-04-30 01:13:49

标签: python tkinter

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

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

任何帮助都会很棒,谢谢。

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()

1 个答案:

答案 0 :(得分:0)

我对熊猫不熟悉,因此我将使用openpyxl进行演示。这可能与大熊猫相似甚至更简单。

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()

快速使用熊猫代替:

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