-2

I am a python and arcpy user and I have a problem about dependent combobox. Actually I have asked the same topic on here, but no one answer yet. I've got the answer from here and here. But I think I am too newbie on python programming and I don't get the answer clearly. I try my code like this below, base on the answer that I've got before:

import Tkinter
from Tkinter import *

root = Tkinter.Tk()

bu = StringVar()
bu.set("")
businessunit = ["DUM", "IND", "KAM", "RAP"]
bu_menu = OptionMenu(root, bu, *businessunit, command=Combobox_1)
bu_menu.config(bg="white", fg="dark blue", width=3, relief=GROOVE)
bu_menu.place(x=95, y=110)

sec = StringVar()
sec.set("")
sector = {"DUM":['GRG', 'KBU', 'LBO', 'PLS', 'PLU', 'PPR', 'RPT', 'SBI', 'SKB'],
                "IND":['BYS','MER','NGD','PER','SJG','SLJ'],
                "KAM":['RSG','SRG','SRY','TSK'],
                "RAP":['BAS','CER','LGB','LON','LOS','MDU','MRE','MRW','PEN','PES','PPD','TEE','TEW','TSB','UKU']}
sec_menu = OptionMenu(root, sec, *sector, command=Combobox_2)
sec_menu.config(bg="white", fg="dark blue", width=3, relief=GROOVE)
sec_menu.place(x=155, y=110)

def __init__(self):

    def Combobox_1(businessunit):
        print bu.get()

    def Combobox_2(sector):
        print sec.get()

    self.Combobox_1.activated[str].connect(self.on_combo_activated)

def on_combo_activated(self, text):
    self.Combobox_2.clear()
    self.Combobox_2.addItems(self.sector[text])

root.pack()
root.mainloop()
root.destroy()

Please anyone help me on this. Thank you so much for your answer.

edited:

in this case, dependent combobox means:

If DUM is selected in Combobox_1, Combobox_2 will only show GRG,KBU,LBO, etc.

If IND is selected in Combobox_1, Combobox_2 will only show BYS,MER,PER, etc.

If KAM is selected in Combobox_1, Combobox_2 will only show RSG,SRG,SRY, etc.

If RAP is selected in Combobox_1, Combobox_2 will only show BAS,CER,LGB, etc.

Community
  • 1
  • 1
Dias
  • 1
  • 2
  • 5
  • 1
    Can you describe your "real" problem in more detail? Please elaborate a little on what you mean by "dependent combobox"... What does your code do, and what would you like it to do? – nostradamus Dec 16 '16 at 07:45

3 Answers3

1

I would advise looking at: Change OptionMenu based on what is selected in another OptionMenu

Just change the values as you need them in your menu.

(Also, you don't need to import Tkinter twice, just use "import Tkinter" or "import Tkinter as tk")

Community
  • 1
  • 1
Marc vT
  • 496
  • 3
  • 12
0

Thank you for your contribution. Actually I've got the answer of this question in my question before. Please see this link Graded combobox Menu Python Tkinter

Community
  • 1
  • 1
Dias
  • 1
  • 2
  • 5
0

This is called a cascade combo box. Basically in your command (action) you then create the second box with the data based on the selected value of the first. This is easy with a database i dont know of other ways but some sort of array or dictionary should also work fine.

I do it in a class that I call from my app so this is a little over complicated but can be easily simplified if you dont want to bother with the class vals is a dictionary that looks like {'value1':number,'value2':number2} to keep the index needed fro a database call to get more values or whatever. I am using .grid you can use pack or whatever suits you.

def ebox(self, index=None, row=None, column=None, vals=None):
        self.value[index] = tk.StringVar()
        lis = []
        for item in vals:
            lis.append(item)
        self.combos[index] = ttk.Combobox(self.win, values=lis, textvariable=self.value[index])
        self.combos[index].bind("<<ComboboxSelected>>", lambda event, y=vals: self.eboxclk(event, y))
        self.combos[index].grid(row=row, column=column)
        return

    def eboxclk(self, event=None, index=None):
        print(event.widget.get(), " widget get ")
        print(index[event.widget.get()], " this should be your index")
        return

I initialize a dictionary for my widgets, but you could just skip it and just

    mycombo = ttk.Combobox(master,values=alist, textvariable=tk.StringVar)
    mycombo.bind('<<ComboboxSelected>>', lambda event, dict: dosomething(event, dict)