简体   繁体   English

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

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

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.在过去的 15 天里,我一直在 python 中编码...我的代码需要一点帮助...该脚本将任何 excel 表(.xlsx)作为输入并生成第一个填充行作为下拉按钮名称,它还列出了该下拉按钮中相应列的值。 I have created the dropdown buttons using Combobox and i have created them using for loop.我使用 Combobox 创建了下拉按钮,并使用 for 循环创建了它们。 The problem is i am able to control only the lastly created Combobox.问题是我只能控制最后创建的 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.我需要的是...在第一个 combobox 中,如果我 select 是索引 2 的元素,那么所有其他以下下拉按钮应该刷新并且只有相应列的第 2 行元素。

for eg:例如:

NAME ITEM PRICE PIECES AVAILABLE名称 项目 价格

Janu biscuit 15 14 Janu 饼干 15 14

Roger cake 35 10罗杰蛋糕 35 10

Fidel Cookie 20 20菲德尔饼干 20 20

Vasu nougat 10 5瓦苏牛轧糖 10 5

An excel file has three sheets(week1, week2, week3) with the same data.一个 excel 文件具有三个具有相同数据的工作表(第 1 周、第 2 周、第 3 周)。

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.将创建一个 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. 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.我无法在此处上传 .xlsx 文件。 But the content is given as given in example and the excel name is items_list.xlsx但内容如示例所示,excel 名称为 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("<<ComboboxSelected>>",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)我创建了一个随机字符串数组...随机字符串数组的每个值都用作每个组合按钮的名称,而不是仅使用所有组合按钮的单个名称(具有单个名称的缺点是它允许用户控制只有最后创建的组合框)

暂无
暂无

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

相关问题 如果组合框在 python gui 中可用,如何销毁它们? - How to destroy comboboxes if they are available in python gui? 我正在尝试使用 tkinter 创建一个带有两个组合框的简单 GUI。 但是第一个组合框选择的值在第二个组合框中被覆盖 - I am trying to create a simple GUI with two combobox using tkinter. But first combo box selected value gets over written in second combobox 如何在 python 中获取 combobox 所选项目的值 - how to get the value of combobox selected item in python Python TkInter - 多个组合框复制彼此的选定值 - Python TkInter - Multiple Comboboxes copying eachother's selected value 获取 Combobox python 的选定值 - getting selected value of Combobox python 如何使用 Tkinter 将网站 URL 分配为 Python GUI 中选定单选按钮的值? - How do I assign a website URL as the value of a selected radiobutton in a Python GUI using Tkinter? 从第一个组合框中选择后,如何从select查询中为其他组合框添加值 - how to add value to the other combobox from select query after selecting from the first combobox 如何通过将其他列表的值用作第一个列表的索引,将一个列表的值插入到另一个列表中? (Python) - How can I insert values of one list into another by using the values of the other lists an index into the first? (Python) python tkinter canvas 根据选定的 combobox 值使用多个条件移动图像 - python tkinter canvas moving an image using multiple condition based on selected combobox values 如何从QTableWidget获取选定的组合框值? - how to get the selected combobox value from QTableWidget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM