Python 如何通过tkinter下拉列表中的输入从excel导入特定单元格值?
我现在有这个代码,我想在其中添加另一个因素,当我从下拉列表中选择一个特定年份,从另一个中选择村庄时,我想要横截面值。e、 g村庄从下拉列表中选择=阿巴德加尔,选择年份=2021,头部标签中的值应为=8.41。类似地,如果我选择2022,它应该是8.02,依此类推。将其分解为几个步骤:首先需要找到包含abad garh的行,然后需要在所选年份的列中获取单元格。如果只查找几次,搜索Excel文件就可以了。如果需要多次搜索,则应将文件解析为数据结构,以便更快地查找。@代码学徒我有1000行35列的数据,但只想在excel中工作,因为我的代码几乎准备好了……。只剩下这一步。我的第一条注释解释了如何实现这一点。Python 如何通过tkinter下拉列表中的输入从excel导入特定单元格值?,python,excel,tkinter,drop-down-menu,openpyxl,Python,Excel,Tkinter,Drop Down Menu,Openpyxl,我现在有这个代码,我想在其中添加另一个因素,当我从下拉列表中选择一个特定年份,从另一个中选择村庄时,我想要横截面值。e、 g村庄从下拉列表中选择=阿巴德加尔,选择年份=2021,头部标签中的值应为=8.41。类似地,如果我选择2022,它应该是8.02,依此类推。将其分解为几个步骤:首先需要找到包含abad garh的行,然后需要在所选年份的列中获取单元格。如果只查找几次,搜索Excel文件就可以了。如果需要多次搜索,则应将文件解析为数据结构,以便更快地查找。@代码学徒我有1000行35列的数据
from tkinter import *
from tkinter import ttk
import openpyxl
from openpyxl import load_workbook
win=Tk()
BLOCK_NAME=StringVar()
VILLAGE_NA=StringVar()
head=StringVar()
# Load the xlsx file, then store the value of each column in the "elements" list
wb = load_workbook(filename=r"C:\Users\Hp\Desktop\Master_Db.xlsx")
ws = wb['Sheet1']
xlsx_range = ws['B2':'B21']
head_range = ws['P1':'AI1']
vill = []
yr=[]
for cell in xlsx_range:
for x in cell:
y = x.value
vill.append(y)
for cell in head_range:
for x1 in cell:
y1=x1.value
yr.append(y1)
def lookupdata(event):
print(combodata.get())
def lookuphead():
value1=comboy.get()
for j in range(16, 20):
for i in range(2, 11):
if value1 == ws.cell(1, j).value:
head.set(ws.cell(i, j).value)
return
def lookupvillage():
value = combodata.get()
for i in range(2, 11):
if value == ws.cell(i, 2).value:
BLOCK_NAME.set(ws.cell(i, 3).value)
TEHSIL_NAM.set(ws.cell(i, 4).value)
DISTRICT_N.set(ws.cell(i,5).value)
return
#couldn't find a matching value. Maybe the blank item is still selected?
BLOCK_NAME.set("unknown")
Label(win, text="Select the Year", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=11, column=0, padx=10, pady=5, sticky='w')
comboy=ttk.Combobox(win, textvariable=optsy, width=20, state='readonly')
comboy['values']=yr
comboy.grid(row=11, column=1,padx=10, pady=5)
comboy.bind("<<ComboboxSelected>>")
Label(win, text="Select data").grid(row=1, column=1, padx=10, pady=10)
combodata=ttk.Combobox(win, textvariable=clicked, width=30)
combodata['values']=elements
combodata.grid(row=1, column=2,padx=50, pady=50)
combodata.bind("<<ComboboxSelected>>", lookupdata)
lbl3=Label(win, text="Block", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=6, column=0, padx=20, pady=5, sticky='w')
ent3=Entry(win, textvariable=BLOCK_NAME, width=20, state='readonly')
ent3.grid(row=6, column=1, padx=20, pady=5)
lbl6=Label(win, text="Head", bg="crimson", fg="white", font=("times new roman",15,"bold")).grid(row=10, column=0, padx=20, pady=10, sticky='w')
ent6=Entry(win, textvariable=head, width=15, state='readonly')
ent6.grid(row=10, column=1, padx=10, pady=10, sticky='w')
btn = Button(win, text = 'Calculate Value',command=lambda:[lookupvillage(), lookuphead()] , bd = '5', width=15, height=2)
btn.grid(row=7, column=2, padx=20, pady=10)
win.mainloop()