简体   繁体   中英

python tkinter fill optionmenu with list from function

I have created a tkinter GUI.

I have bound a function def scrape to a button. When I press the button, selenium will get data from different elements and stores them in different lists.

In my GUI I want to have an OptionMenu for each of the lists.

The problem is, the list is created after I press the button.

When I want to add the OptionMenu to the GUI and load a list as values it gets me an error there is no variable "list". This is because when app.mainloop() starts there is not list created yet.

This s my scraping code:

def scrape():
    li_leagues = []
        print('Getting leagues...\n')
        #click the dropdown menue to open the folder
        league_dropdown_menu = driver.find_element_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)

This is what I tried for the OptionMenu :

str_quality = StringVar()
str_quality.set(li_quality[0])

#Dropdown
d_quality = OptionMenu(app, str_quality, *li_quality)

How can i make it working that I can add the OptionMenu to my GUI?

EDIT:

I added the name of the list as an empty list li_quality = [] to the code before app.mainloop() gets called.

The GUI is loading now with the OptionMenu. But it doesnt get updated after I scraped my data.

Im not sure if this is what you want, but i found something on the internet and ive modified it a bit, take a look:

from tkinter import *

root = Tk()

def hey():
    lst = [1,2,3,4]
    m = a.children['menu']
    for val in lst:
        m.add_command(label=val,command=lambda v=sv,l=val:v.set(l))

sv = StringVar()
dummy = []
a = OptionMenu(root,sv,*dummy)
a.pack()

b = Button(root,text='Click me',command=hey)
b.pack()

root.mainloop()

Here, initially the a is set to an empty list, but after you click the button, it'll be populated from the list.

Here is the link to the discussion

Alternative Method:

Anyway, what I recommend is to create the OptionMenu inside of a function later on, after receiving the items, or go for some other widget capable of doing it. I am pretty sure it is possible with ttk.Combobox .

Take a look below on how to do so with Combobox :

from tkinter import *
from tkinter import ttk

root = Tk()

def hey():
    lst = [1,2,3,4]
    a.config(value=lst)

dummy = []
a = ttk.Combobox(root,value=dummy,state='readonly')
a.pack()

b = Button(root,text='Click me',command=hey)
b.pack()

root.mainloop()

Hope it helped you get some idea, do let me know if any errors.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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