每次按下 "refresh" 时如何用新数据填充 tkinter 下拉框?
How do i populate tkinter drop-down box with new data everytime i pressed "refresh"?
我有一个刷新按钮,每次按下它都会从数据库中获取最新数据。我可以从终端看到新数据,但数据无法显示在下拉列表中。
例如:
每次我按“刷新”时,终端都会显示新数据“newadded”:
但是“新添加的”数据无法显示在 tkinter 下拉列表中:
我的代码:
def MainMenuForm():
refresh_button = Button(root, text="refresh", command=ListboxContent)
refresh_button.place(x=130, y=17, width=50, height=22)
JsonPresetLBL = Label(jsonframe, text="JSON Preset:", font=("Calibri", 11, "bold"), fg="black")
JsonPresetLBL.place(x=200, y=170)
global options
options = StringVar(jsonframe)
options.set("Select ") # default value
om1 = OptionMenu(jsonframe, options, *jsonprofileName, command=get)
om1.place(x=290, y=168, width=100)
def ListboxContent():
# ==========jsonProfileName================
cur = con.cursor()
sqlName = ("select jsonid, jsonprofilename from json")
# call jsonProfileName
global jsonprofileName
jsonprofileName = []
try:
cur.execute(sqlName)
results = cur.fetchall()
for a in results:
global data
data = (a[1])
jsonprofileName.append(data)
print(data)
except:
print("Error: unable to fetch data")
一种方法是摧毁旧的并在相同位置放置新的。
如果您使用place
,那么您只需将新的放在同一坐标即可,例如
from tkinter import *
def refresh():
global optionmenu
data=['new','data']
optionmenu.destroy()
option.set('')
optionmenu=OptionMenu(root,option,*data)
optionmenu.place(x=100,y=50)
root=Tk()
option=StringVar()
data=['hello','world']
optionmenu=OptionMenu(root,option,*data)
optionmenu.place(x=100,y=50)
button=Button(root,text='Refresh',command=refresh)
button.pack(padx=100,pady=100)
root.mainloop()
如果您使用 pack
或 grid
,您将需要一个容器 Frame
来容纳该位置,例如
from tkinter import *
def refresh():
global optionmenu
data=['new','data']
optionmenu.destroy()
option.set('')
optionmenu=OptionMenu(op_frame,option,*data)
optionmenu.pack()
root=Tk()
option=StringVar()
data=['hello','world']
op_frame=Frame(root)
op_frame.pack()
optionmenu=OptionMenu(op_frame,option,*data)
optionmenu.pack()
button=Button(root,text='Refresh',command=refresh)
button.pack(padx=100,pady=10)
root.mainloop()
更新
您也可以访问 OptionMenu
的 menu
,清除它并重写所有选项。
def refresh():
global optionmenu
data=['new','data']
menu=optionmenu['menu']
menu.delete(0,END)
for d in data:
menu.add_command(label=d,command=lambda val=d: option.set(val))
我有一个刷新按钮,每次按下它都会从数据库中获取最新数据。我可以从终端看到新数据,但数据无法显示在下拉列表中。
例如: 每次我按“刷新”时,终端都会显示新数据“newadded”:
但是“新添加的”数据无法显示在 tkinter 下拉列表中:
我的代码:
def MainMenuForm():
refresh_button = Button(root, text="refresh", command=ListboxContent)
refresh_button.place(x=130, y=17, width=50, height=22)
JsonPresetLBL = Label(jsonframe, text="JSON Preset:", font=("Calibri", 11, "bold"), fg="black")
JsonPresetLBL.place(x=200, y=170)
global options
options = StringVar(jsonframe)
options.set("Select ") # default value
om1 = OptionMenu(jsonframe, options, *jsonprofileName, command=get)
om1.place(x=290, y=168, width=100)
def ListboxContent():
# ==========jsonProfileName================
cur = con.cursor()
sqlName = ("select jsonid, jsonprofilename from json")
# call jsonProfileName
global jsonprofileName
jsonprofileName = []
try:
cur.execute(sqlName)
results = cur.fetchall()
for a in results:
global data
data = (a[1])
jsonprofileName.append(data)
print(data)
except:
print("Error: unable to fetch data")
一种方法是摧毁旧的并在相同位置放置新的。
如果您使用place
,那么您只需将新的放在同一坐标即可,例如
from tkinter import *
def refresh():
global optionmenu
data=['new','data']
optionmenu.destroy()
option.set('')
optionmenu=OptionMenu(root,option,*data)
optionmenu.place(x=100,y=50)
root=Tk()
option=StringVar()
data=['hello','world']
optionmenu=OptionMenu(root,option,*data)
optionmenu.place(x=100,y=50)
button=Button(root,text='Refresh',command=refresh)
button.pack(padx=100,pady=100)
root.mainloop()
如果您使用 pack
或 grid
,您将需要一个容器 Frame
来容纳该位置,例如
from tkinter import *
def refresh():
global optionmenu
data=['new','data']
optionmenu.destroy()
option.set('')
optionmenu=OptionMenu(op_frame,option,*data)
optionmenu.pack()
root=Tk()
option=StringVar()
data=['hello','world']
op_frame=Frame(root)
op_frame.pack()
optionmenu=OptionMenu(op_frame,option,*data)
optionmenu.pack()
button=Button(root,text='Refresh',command=refresh)
button.pack(padx=100,pady=10)
root.mainloop()
更新
您也可以访问 OptionMenu
的 menu
,清除它并重写所有选项。
def refresh():
global optionmenu
data=['new','data']
menu=optionmenu['menu']
menu.delete(0,END)
for d in data:
menu.add_command(label=d,command=lambda val=d: option.set(val))