如何从 tkinter 中的 excel 值下拉?
How to make a drop down from excel values in tkinter?
from openpyxl import load_workbook
wb = load_workbook(filename=r'C:\Users\Hp\Desktop\Test data.xlsx' )
ws = wb['Sheet1']
range=ws['B2':'B10']
for cell in range:
for x in cell:
y=x.value
print(y)
win=Tk()
clicked=StringVar()
combodata=ttk.Combobox(win, width=100,state='readonly')
combodata['values']=[(y)]
combodata.pack(pady=20)
win.mainloop()
我准备了这段代码。这会打印列中的所有值,但是当涉及到 tkinter-dropdown 时,它只提供所提供范围内最后一个值的选项,即仅 B10。你们中的任何人都可以根据我的需要修改此代码吗?我在列中有超过 1000 个值。
提前致谢。
创建一个空列表,然后将每个值附加到列中。另外,请确保您没有使用保留关键字作为变量。
代码:
from openpyxl import load_workbook
from tkinter import *
import tkinter.ttk as ttk
# Load the xlsx file, then store the value of each column in the "elements" list
wb = load_workbook(filename=r"C:\Users\Hp\Desktop\Test data.xlsx")
ws = wb['Sheet1']
xlsx_range = ws['B2':'B10']
elements = []
for cell in xlsx_range:
for x in cell:
y = x.value
elements.append(y)
print(y)
# Tkinter stuff
win = Tk()
clicked = StringVar()
combodata = ttk.Combobox(win, width=100,state='readonly', values=elements)
combodata.pack(pady=20)
win.mainloop()
from openpyxl import load_workbook
wb = load_workbook(filename=r'C:\Users\Hp\Desktop\Test data.xlsx' )
ws = wb['Sheet1']
range=ws['B2':'B10']
for cell in range:
for x in cell:
y=x.value
print(y)
win=Tk()
clicked=StringVar()
combodata=ttk.Combobox(win, width=100,state='readonly')
combodata['values']=[(y)]
combodata.pack(pady=20)
win.mainloop()
我准备了这段代码。这会打印列中的所有值,但是当涉及到 tkinter-dropdown 时,它只提供所提供范围内最后一个值的选项,即仅 B10。你们中的任何人都可以根据我的需要修改此代码吗?我在列中有超过 1000 个值。 提前致谢。
创建一个空列表,然后将每个值附加到列中。另外,请确保您没有使用保留关键字作为变量。
代码:
from openpyxl import load_workbook
from tkinter import *
import tkinter.ttk as ttk
# Load the xlsx file, then store the value of each column in the "elements" list
wb = load_workbook(filename=r"C:\Users\Hp\Desktop\Test data.xlsx")
ws = wb['Sheet1']
xlsx_range = ws['B2':'B10']
elements = []
for cell in xlsx_range:
for x in cell:
y = x.value
elements.append(y)
print(y)
# Tkinter stuff
win = Tk()
clicked = StringVar()
combodata = ttk.Combobox(win, width=100,state='readonly', values=elements)
combodata.pack(pady=20)
win.mainloop()