如何使用在python gui中的第一个组合框中选择的值来控制其他组合框的值? [英] How to control the values of other comboboxes using the value selected in first combobox in python gui?
问题描述
过去15天我一直在用python编码...我的代码需要一点帮助...
该脚本以任何excel工作表(.xlsx)为输入并生成第一个填充的行作为具有相同名称的下拉按钮,它还会列出该下拉按钮中相应列的值。我已经使用Combobox创建了下拉按钮,并且已经使用for循环创建了它们。问题是我只能控制最后创建的组合框。
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.
我需要的是...如果我选择索引2的元素,则在第一个组合框中。接下来的所有其他下拉按钮都应该刷新,并且只有相应列的第2行元素。
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.
例如:
有现货的商品价格
Janu饼干15 14
Janu biscuit 15 14
罗杰蛋糕35 10
Fidel Cookie 20 20
Fidel Cookie 20 20
Vasu牛轧糖10 5
Vasu nougat 10 5
文件具有三张具有相同数据的表单(week1,week2,week3)。
An excel file has three sheets(week1, week2, week3) with the same data.
将创建一个GUI,其中NAME,ITEM,PRICE,PIECES AVAILABLE(标题)作为下拉列表按钮,将显示每个标题的所有列元素。如果我从组合框中选择第1列的Roger,则Item组合框应刷新索引为1的项作为蛋糕,并且价格组合框应仅包含35作为价格,依此类推。
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.
我无法在此处上传.xlsx文件。但是内容是按示例给出的,excel名称是items_list.xlsx
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
代码是
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("<<ComboboxSelected>>",refresh_drop)
TAB_CONTROL.pack(expand=1, fill="both")
window.mainloop()
请帮助我
推荐答案
我创建了一个随机的字符串数组...每个随机按钮的名称用作每个组合按钮的名称,而不是对所有组合按钮仅使用一个名称(使用单个名称的缺点是它允许用户只能控制最后创建的组合框)
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)
这篇关于如何使用在python gui中的第一个组合框中选择的值来控制其他组合框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!