Search code examples
pythonlistvariablestkintercombobox

Python ttk combobox value update?


I use a GUI for some web automation with selenium. I have several comboboxes (will do one example here)

This is my example code:

app = Tk()


def callback(*args): # should get the updated values in Combobox ?
    global v_sv_league
    v_sv_league = str(sv_league.get())


#List to be filled by scraper
li_leagues = []

#StringVar
sv_league = StringVar()
sv_league.trace("w", callback)

#Label
l_d_league = tk.Label(app,text='League:',bg='#1d1b29', fg='#f8f09d',font='Tahoma 10')

#Combobox
d_league = ttk.Combobox(app,textvariable=sv_league,values=li_leagues)


#Scrape
def scrape():
       
        btn_tflist = wait.until(EC.element_to_be_clickable((By.XPATH,('/html/body/main/section/nav/button[3]'))))
        btn_tflist.click()

        btn_tf_filters = wait.until(EC.element_to_be_clickable((By.XPATH,'/html/body/main/section/section/div[2]/div/div/div[2]/div[2]')))
        btn_tf_filters.click()

        bol_scrape = True
        if bol_scrape is True:
            print('\n Start scraping... this might take a few minutes. Please wait and dont press anything until trade_buddy is done!\n')

            li_leagues = []
            print('Getting leagues...\n')
            league_dropdown_menu = wait.until(EC.element_to_be_clickable((By.XPATH,('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[7]/div'))))
            league_dropdown_menu.click()
            time.sleep(1)
            
            # scrape all text
            scrape_leagues = driver.find_elements_by_xpath("//li[@class='with-icon' and contains(text(), '')]")
            for league in scrape_leagues:
                export_league = league.text
                export_league = str(export_league)
                export_league = export_league.replace(',', '')
                li_leagues.append(export_league)


app.mainloop()

So basically this is just a small part of my code but this is what I got for one of my combobox's. You can see that I will call for def scrape at some point in my code to scrape data and to fill my list li_leagues. However, my combobox is not refreshing the content and stays empty.

For OptionMenu I got it set up (with a it other code) but I cant get it working with combobox.

Any advice what I am missing here?

Thanks a slot!


Solution

  • Try using this line of code, after appending the list with values.

    .....
    export_league = export_league.replace(',', '')
    li_leagues.append(export_league)
    c_league.config(values=li_leagues)
    

    config() method acts as a updater that just updates your widget, when called.

    Hope it was of some help.

    Cheers