如何使用 python gui 中第一个组合框中选择的值来控制其他组合框的值?
How to control the values of other comboboxes using the value selected in first combobox in python gui?
过去 15 天我一直在 python 编码...我的代码需要一点帮助...
该脚本将任何 excel sheet(.xlsx) 作为输入并生成第一个填充行作为具有相同名称的下拉按钮,它还列出了该下拉按钮中相应列的值。我使用 Combobox 创建了下拉按钮,并使用 for 循环创建了它们。问题是我只能控制最后创建的组合框。
我需要的是...在第一个组合框中,如果我 select 索引 2 的元素,则所有其他后续下拉按钮应该刷新并且只有相应列的第 2 行元素。
例如:
可用的名称商品价格件数
Janu 饼干 15 14
罗杰蛋糕 35 10
菲德尔饼干 20 20
瓦苏牛轧糖 10 5
一个 excel 文件有三个 sheets(week1, week2, week3) 具有相同的数据。
将使用 NAME、ITEM、PRICE、PIECES AVAILABLE(headers) 创建一个 GUI 作为下拉按钮,它将显示每个 header 的所有列元素。 IF i select Roger of column 1 from combobox, Item combobox should refresh the items with index 1 as cake and similarly the price combobox should have only 35 as price.
我无法在此处上传 .xlsx 文件。但内容如示例所示,excel 名称为 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()
请帮我解决这个问题
我创建了一个随机字符串数组...随机字符串数组的每个值都用作每个组合按钮的名称,而不是只为所有组合按钮使用一个名称(使用单个名称的缺点是允许用户仅控制最后创建的组合框)
过去 15 天我一直在 python 编码...我的代码需要一点帮助... 该脚本将任何 excel sheet(.xlsx) 作为输入并生成第一个填充行作为具有相同名称的下拉按钮,它还列出了该下拉按钮中相应列的值。我使用 Combobox 创建了下拉按钮,并使用 for 循环创建了它们。问题是我只能控制最后创建的组合框。
我需要的是...在第一个组合框中,如果我 select 索引 2 的元素,则所有其他后续下拉按钮应该刷新并且只有相应列的第 2 行元素。
例如:
可用的名称商品价格件数
Janu 饼干 15 14
罗杰蛋糕 35 10
菲德尔饼干 20 20
瓦苏牛轧糖 10 5
一个 excel 文件有三个 sheets(week1, week2, week3) 具有相同的数据。
将使用 NAME、ITEM、PRICE、PIECES AVAILABLE(headers) 创建一个 GUI 作为下拉按钮,它将显示每个 header 的所有列元素。 IF i select Roger of column 1 from combobox, Item combobox should refresh the items with index 1 as cake and similarly the price combobox should have only 35 as price.
我无法在此处上传 .xlsx 文件。但内容如示例所示,excel 名称为 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()
请帮我解决这个问题
我创建了一个随机字符串数组...随机字符串数组的每个值都用作每个组合按钮的名称,而不是只为所有组合按钮使用一个名称(使用单个名称的缺点是允许用户仅控制最后创建的组合框)