python - list tkinter combobox options with suboptions -


not sure if i've seen done before combobox without building custom one. i'm building own ebay spider , want user able select list of categories crawl on queries etc. each category has it's own subcategories of course. , can't explicitly select subcategory of category in ebay's advanced search form. however, can send subcategory id form submission request automatically search within subcategory.

after crawling on categories on ebays site getting categories -> subcategories category have dictionary looks along lines of this:

categories = {main_category_title: [sub_category1, ...., sub_categoryn],               ....} 

i know how subcategories listed combobox doing this:

self.categories = ttk.combobox(self,               values = list(itertools.chain.from_iterable(               (ebay_spider.categories[category] category in ebay_spider.categories))))  

while works... it's not want since doesn't allow user select main category containing each subcategory , looks rather convoluted.

is there way list main categories subcategories below them? preferrably indented.

approach 1: alternative thought of make textbox button inside each category / subcategory sets tk.stringvar containing value since can handle indentation way, prefer not have if there's simpler approach.

approach 2: when getting sub categories " " (some arbitrary amount of whitespace) + subcategory while putting them list.

this "hackish" imo , when using later grabbing subcategories i'd have call .strip(). works @ caveat of having use .strip() when using subcategories elsewhere

here's quick example of using 2 combo boxes. main category uses stringvar() trace when changed updates values of second category. if user enters not found in dict of categories blank value of '--none--' set instead:

from tkinter import * import ttk  def main_change(*args):     second.set('--none--')     second['values'] = categories.get(main_selected.get(), ['--none--'])  categories = {'fruit': ['apples', 'grapes', 'bananas'], 'vegetables': ['peas', 'carrots']}  root = tk()  main_selected = stringvar() main_selected.trace('w', main_change)  main = ttk.combobox(root, values=list(categories.keys()), textvariable=main_selected) main.pack() second = ttk.combobox(root, values=['--none--']) second.pack()  root.mainloop() 

there couple of other ways of achieving this, depending on application may want them, 1 using postcommand() method of combobox().


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -