Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tkinter中将excel行显示为网格_Python_Tkinter - Fatal编程技术网

Python 在tkinter中将excel行显示为网格

Python 在tkinter中将excel行显示为网格,python,tkinter,Python,Tkinter,我在384孔板上对荧光细胞成像,我的软件对数据进行了格式化的excel分析(16行24列图像变成了数据列表,每个孔有2个测量值,约800个数据点)。因为有很多与数据的手动交互,所以我想通过获取excel工作表中索引的信息并将其映射为tkinter网格来自动化我的工作。我想将我的数据从列表中格式化回原来的16x24显示。除其他外,我希望能够与这些数据交互,但作为python的新手,我很难映射这些数据 我使用pandas读取数据帧,但似乎无法将列表显示到相应的网格框中。理想情况下,我希望excel列

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

我使用pandas读取数据帧,但似乎无法将列表显示到相应的网格框中。理想情况下,我希望excel列表中的值显示在tkinter网格中相应的单元格中。例如,在我的excel列表中,[1]:,列出了每口井的两个数据点。我想从每个单元格中获取值,并将它们显示到tkinter中相应的网格中,因此“A1”值的平均值将映射到网格的第1列第1行,A2将是第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()
从tkinter导入*
作为pd进口熊猫
来自《熊猫》的作者
从导入Excel文件
df=pd.read\u excel('/Users/Zammam/PycharmProjects/Filipin\u Analysis\u Zammam/data.xlsx',sheet\u name='Dataps')
打印(()
类单元格():
填充的\u颜色\u BG=“绿色”
空\u COLOR\u BG=“白色”
填充颜色边框=“绿色”
空的\u颜色\u边框=“黑色”
定义初始(自、主、x、y、大小):
self.master=master
self.abs=x
self.ord=y
self.size=size
self.fill=False
def_开关(自):
“”“如果单元格已满或未满,请切换。”“”
self.fill=非self.fill
def牵引(自):
如果self.master!=无:
填充=单元格填充\u颜色\u背景
轮廓=单元格填充\颜色\边框
如果不是自行填写:
填充=单元格。空\u颜色\u背景
轮廓=单元格。空\u颜色\u边框
xmin=self.abs*self.size
xmax=xmin+self.size
ymin=self.ord*self.size
ymax=ymin+self.size
self.master.create_矩形(xmin,ymin,xmax,ymax,fill=fill,outline=outline)
类别CellGrid(画布):
定义初始值(self、master、rowNumber、columnNumber、cellSize、*args、**kwargs):
画布._uuinit_uuu(self,master,width=cellSize*columnNumber,height=cellSize*rowNumber,*args,**kwargs)
self.cellSize=cellSize
self.grid=[]
对于范围内的行(行编号):
行=[]
对于范围内的列(columnNumber):
行。追加(单元格(自身、列、行、单元格大小))
self.grid.append(行)
#记住经过修改的细胞,以避免在鼠标移动过程中出现多次状态切换。
self.switched=[]
#绑定单击操作
self.bind(“,self.handleMouseClick)
#单击时绑定移动
self.bind(“,self.handleMouseMotion)
#绑定释放按钮操作-清除midified单元格的内存。
self.bind(“,lambda事件:self.switched.clear())
self.draw()
def牵引(自):
对于self.grid中的行:
对于行中的单元格:
cell.draw()
def创建_文本(自):
对于self.grid中的行:
对于行中的单元格:
对于df中的i:
cell.create_text(text=df)
def_eventCoords(自我、事件):
行=int(event.y/self.cellSize)
column=int(event.x/self.cellSize)
返回行、列
def handleMouseClick(自身,事件):
行、列=自身。\u事件坐标(事件)
cell=self.grid[行][列]
电池。_开关()
cell.draw()
#将单元格添加到单击期间切换的单元格列表中
self.switched.append(单元格)
def handleMouseMotion(自身、事件):
行、列=自身。\u事件坐标(事件)
cell=self.grid[行][列]
如果电池未处于自切换模式:
电池。_开关()
cell.draw()
self.switched.append(单元格)
如果名称=“\uuuuu main\uuuuuuuu”:
app=Tk()
grid=CellGrid(应用程序16、24、15)
grid.pack()
app.mainloop()

我不熟悉熊猫,所以我将使用
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

如果您删除与标签交互的部分,然后只显示您试图提取excel数据并对其进行网格化的代码,以及从中获取错误的代码,这会更好。您好,我正试图准确地理解这一点--如何获取这些excel数据并在tkinter网格上显示每个单元格的值。我目前没有我不知道该怎么做。def create_text(self):for self中的行。grid:for row中的单元格:for df中的i:cell。create_text(text=df)