How to make a drop down from excel values in tkinter?

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

I have prepared this code. This prints all values in the column but when it comes to tkinter-dropdown, it just provides the option of last value in the range provided i.e. B10 only. Can any of you please modify this code according to my need? I have more than 1000 values in the column. Thanks in advance.

1 Answers

Make an empty list, then append each value in the column. Also, be sure you don't use reserved keywords as variables.

Code:

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