I have been coding in python for the past 15 days...I need a small help with my code...
the script which takes any excel sheet(.xlsx) as input and generates the first filled row as dropdown buttons with the same name and it also lists the values of the corresponding columns in that dropdown button. I have created the dropdown buttons using Combobox and i have created them using for loop. The problem is i am able to control only the lastly created Combobox.
What i need is...in the first combobox if i select a element of index 2, then all other following dropdown buttons should refresh and have only the row 2 elements of the corresponding columns.
for eg:
NAME ITEM PRICE PIECES AVAILABLE
Janu biscuit 15 14
Roger cake 35 10
Fidel Cookie 20 20
Vasu nougat 10 5
An excel file has three sheets(week1, week2, week3) with the same data.
a GUI will be created with NAME, ITEM, PRICE , PIECES AVAILABLE(headers) as dropdown buttons which will display all the column elemnts of each header. IF i select Roger of column 1 from combobox, the Item combobox should refresh the items with index 1 as cake and similarly the price combobox should have only 35 as price and so on.
I am not able to upload the .xlsx file here. But the content is given as given in example and the excel name is items_list.xlsx
The code is
import tkinter as tk
from tkinter import ttk
import os
import openpyxl
from openpyxl.chart import BarChart3D,Reference
from pandas import DataFrame
from tkinter import messagebox
#update the path here
path = "C:\\Users\\anony\\Desktop"
#checking for the path
try:
os.chdir(path)
print("path changed")
print(os.getcwd())
# Caching the exception
except:
print("Path not found")
import openpyxl as py
from openpyxl.chart import BarChart,Reference
#update the file name
file = "items_list.xlsx"
wb = py.load_workbook(filename=file , read_only = False)
sheets = wb.sheetnames
print (sheets)
ws = wb.active
# intializing the window
window = tk.Tk()
window.title(file)
# configuring size of the window
window.geometry('350x200')
#Create Tab Control
TAB_CONTROL = ttk.Notebook(window)
def OptionCallBack(*args):
#print (header_data.get())
print (header_sheet.current())
def refresh_drop(event):
selected_index = header_sheet.current()
selected_value = header_sheet.get()
print ("selected_index")
print (selected_index)
print ("selected value")
print (selected_value)
#finding max_row, header_row of each sheet
for m in range (0,len(sheets)):
TAB1 = ttk.Frame(TAB_CONTROL)
TAB_CONTROL.add(TAB1, text=sheets[m])
sheet = wb[sheets[m]]
m_row = sheet.max_row
m_col= sheet.max_column
for i in range(1, m_row + 1):
cell_obj = sheet.cell(i,1) #cell(row,column)
print (cell_obj.value)
if(cell_obj.value is not None):
print ("it comes")
header_row=i
print (header_row)
break
value_list = []
for n in range(1,m_col+1):
cell_obj = sheet.cell(header_row,n)
header_row_values = cell_obj.value
if (header_row_values is not None):
value_list.append(header_row_values)
for o in range (header_row+1,m_row+1):
cell_obj1 = sheet.cell(o,n)
values_of_that_col = cell_obj1.value
value_list.append(values_of_that_col)
header_data = tk.StringVar()
header_data.trace('w',OptionCallBack)
value = header_data.get()
header_sheet= ttk.Combobox(TAB1,width= 15,textvariable = value,values = value_list)
#value_list.clear()
header_sheet.grid(column = n, row =1)
header_sheet.current(0)
#global value_list_copy
value_list_copy.append(value_list)
value_list.clear()
#global selected_row
#global selected_col
# Tab_selected = TAB_CONTROL.tab(TAB_CONTROL.select(), "text")
# print(Tab_selected)
# for m in range (0,len(sheets)):
# if (sheets[m] == Tab_selected):
# header_sheet1.bind("<>",refresh_drop)
TAB_CONTROL.pack(expand=1, fill="both")
window.mainloop()
Please help me with this
解决方案
I created a random array of strings...each value of the randomstring array is used as a name for each combobutton instead of using only A single name for all the combo buttons(the disadvantage of having single name is it allows the user to control only the lastly created combobox)