繁体   English   中英

如何使用 python gui 中第一个 combobox 中选择的值来控制其他组合框的值?

[英]How to control the values of other comboboxes using the value selected in first combobox in python gui?

在过去的 15 天里,我一直在 python 中编码...我的代码需要一点帮助...该脚本将任何 excel 表(.xlsx)作为输入并生成第一个填充行作为下拉按钮名称,它还列出了该下拉按钮中相应列的值。 我使用 Combobox 创建了下拉按钮,并使用 for 循环创建了它们。 问题是我只能控制最后创建的 Combobox。

我需要的是...在第一个 combobox 中,如果我 select 是索引 2 的元素,那么所有其他以下下拉按钮应该刷新并且只有相应列的第 2 行元素。

例如:

名称 项目 价格

Janu 饼干 15 14

罗杰蛋糕 35 10

菲德尔饼干 20 20

瓦苏牛轧糖 10 5

一个 excel 文件具有三个具有相同数据的工作表(第 1 周、第 2 周、第 3 周)。

将创建一个 GUI,其中 NAME、ITEM、PRICE、PIECES AVAILABLE(headers) 作为下拉按钮,它将显示每个 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

代码是

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

请在这件事上给予我帮助

我创建了一个随机字符串数组...随机字符串数组的每个值都用作每个组合按钮的名称,而不是仅使用所有组合按钮的单个名称(具有单个名称的缺点是它允许用户控制只有最后创建的组合框)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM