python tkinter 使用函数列表填充选项菜单
python tkinter fill optionmenu with list from function
我创建了一个 tkinter GUI。
我已将函数 def scrape
绑定到按钮。
当我按下按钮时,selenium 会从不同的元素中获取数据并将它们存储在不同的列表中。
在我的 GUI 中,我希望每个列表都有一个 OptionMenu
。
问题是,列表是在我按下按钮后创建的。
当我想将 OptionMenu
添加到 GUI 并将列表作为值加载时,它给我一个错误,没有变量“列表”。这是因为 app.mainloop()
启动时还没有创建列表。
这是我的抓取代码:
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)
这是我为 OptionMenu
尝试的:
str_quality = StringVar()
str_quality.set(li_quality[0])
#Dropdown
d_quality = OptionMenu(app, str_quality, *li_quality)
如何才能将 OptionMenu
添加到我的 GUI 中?
编辑:
我在调用 app.mainloop()
之前将列表的名称作为空列表 li_quality = []
添加到代码中。
GUI 正在加载 OptionMenu。但是在我抓取数据后它没有更新。
我不确定这是不是你想要的,但是我在网上找到了一些东西,我稍微修改了一下,看看:
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()
这里,最初 a
设置为一个空列表,但在您单击该按钮后,它将从列表中填充。
Here is the link to the discussion
替代方法:
无论如何,我建议稍后在收到项目后在函数内部创建 OptionMenu
,或者使用其他能够执行此操作的小部件。我很确定 ttk.Combobox
.
是可能的
看看下面如何使用 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()
希望对你有所帮助,如有错误请告诉我。
干杯
我创建了一个 tkinter GUI。
我已将函数 def scrape
绑定到按钮。
当我按下按钮时,selenium 会从不同的元素中获取数据并将它们存储在不同的列表中。
在我的 GUI 中,我希望每个列表都有一个 OptionMenu
。
问题是,列表是在我按下按钮后创建的。
当我想将 OptionMenu
添加到 GUI 并将列表作为值加载时,它给我一个错误,没有变量“列表”。这是因为 app.mainloop()
启动时还没有创建列表。
这是我的抓取代码:
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)
这是我为 OptionMenu
尝试的:
str_quality = StringVar()
str_quality.set(li_quality[0])
#Dropdown
d_quality = OptionMenu(app, str_quality, *li_quality)
如何才能将 OptionMenu
添加到我的 GUI 中?
编辑:
我在调用 app.mainloop()
之前将列表的名称作为空列表 li_quality = []
添加到代码中。
GUI 正在加载 OptionMenu。但是在我抓取数据后它没有更新。
我不确定这是不是你想要的,但是我在网上找到了一些东西,我稍微修改了一下,看看:
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()
这里,最初 a
设置为一个空列表,但在您单击该按钮后,它将从列表中填充。
Here is the link to the discussion
替代方法:
无论如何,我建议稍后在收到项目后在函数内部创建 OptionMenu
,或者使用其他能够执行此操作的小部件。我很确定 ttk.Combobox
.
看看下面如何使用 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()
希望对你有所帮助,如有错误请告诉我。
干杯