简体   繁体   English

在通过“打开文件”菜单选择并加载文件后,如何在tkinter GUI中将Excel单元格值加载到空文本框中?

[英]How to load Excel cell values onto empty textboxes in a tkinter GUI, after the file has been selected by and “Open File” menu and loaded?

I have constructed a GUI with tkinter. 我已经用tkinter构建了一个GUI。 There are two buttons, one to load an excel sheet and parse all cells and print their value. 有两个按钮,一个用于加载excel工作表并解析所有单元格并打印其值。 Also, I have a series of empty textboxes with headers. 另外,我有一系列带有标题的空文本框。 What I am trying to achieve is to load the parsed excel cells each onto a variable, then fill the empty textboxes with the cell value (ie the variable in question). 我想要实现的是将每个已解析的excel单元加载到一个变量上,然后用单元格值(即所讨论的变量)填充空白文本框。 Any pointers would be greatly appreciated. 任何指针将不胜感激。 Here's my code so far: 到目前为止,这是我的代码:

#!/usr/bin/python3

from tkinter import filedialog
from tkinter import *
import openpyxl

from openpyxl import load_workbook

#Define Window Geometry

main = Tk()
main.geometry("1024x768")
main.title("Window Title")

#Define Empty Cells to be Filled in by Excel File & Calculation

def OpenDataInputSpreadsheetCallBack():
    main.iconify()
    file_path = filedialog.askopenfilename(initialdir = "file_path_goes_here",title = "Choose Input Spreadsheet",filetypes = (("Excel 2010 files","*.xlsx"),("Excel 2003 Files","*.xls")))
    wb = load_workbook(filename = file_path, read_only=True)
    ws = wb.active
    for row in ws.iter_rows():
        for cell in row:
            if (cell.value==None):
                pass
            else:
                print(cell.value)
#
#
#Function to display empty rows and columns              
#
height = 5
width = 6
for x in range(1,height+1): #Rows
   for y in range(width): #Columns
        b = Entry(main, text='')
        b.grid(row=x, column=y)    
#      
# Define Buttons    

b1 = Button(main, text = "Open Data Input Spreadsheet", command = OpenDataInputSpreadsheetCallBack)
b1.place(x = 1,y = 120)
b2 = Button(main, text='Quit', command=main.destroy)
b2.place(x = 1,y = 150)
##
##
### Initialize Column Headers
Label(main, text="Header1").grid(row=0, column=0, sticky=W)
Label(main, text="Header2").grid(row=0, column=1, sticky=W)
Label(main, text="Header3").grid(row=0, column=2, sticky=W)
Label(main, text="Header4").grid(row=0, column=3, sticky=W)
Label(main, text="Header5").grid(row=0, column=4, sticky=W)
Label(main, text="Header6").grid(row=0, column=5, sticky=W)
###
# Define a function to close the window.
def quit(event=None):
    main.destroy()
# Cause pressing <Esc> to close the window.
main.bind('<Escape>', quit)
#
#
main.mainloop()

Question : What I am trying to achieve is to load the parsed excel cells each onto a variable, then fill the empty textboxes with the cell value 问题 :我要实现的是将已解析的excel单元格分别加载到变量上,然后用单元格值填充空白文本框

You don't have to use a variable , you can pass the cell values direct to the textbox. 您不必使用variable ,可以将cell values直接传递到文本框。
For instance: 例如:

class Textbox(object):
    text = None

series_of_textboxes = [Textbox(),Textbox(),Textbox(),Textbox()]
# start reading from row 2
for i, row in enumerate( ws.iter_rows(min_row=2) ):
    series_of_textboxes[i].text = ' '.join(cell.value for cell in row)

print( series_of_textboxes[0].text )  

Output : 输出

Bundesliga 27.08.16 Hamburg Ingolstadt 德甲联赛27.08.16汉堡因戈尔施塔特

Tested with Python:3.4.2 - openpyxl:2.4.1 使用Python:3.4.2测试-openpyxl:2.4.1
Come back and Flag your Question as answered if this is working for you or comment why not. 返回,如果这对您有用,则将您的问题标记为已回答,或者评论为什么不这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM