How do i populate tkinter drop-down box with new data everytime i pressed "refresh"?

Viewed 267

I have a refresh button that will get the latest data from database everytime i pressed. I can see the new data from terminal but the data couldn't show in the drop down list.

For example: New data "newaddded" can be shown in terminal every time i pressed "refresh" :

enter image description here

BUT the "newadded" data cannot be shown in tkinter drop down list : enter image description here

My code:

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")

1 Answers

An approach could be to destroy the old one and place a new one at the same position.

If you use place, then you can simply place the new one at the same coordinate, example

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()

If you use pack or grid, you will need to have a container Frame that will hold the position, example

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()

UPDATE

You could also do it by accessing the menu of the OptionMenu, clearing it out and rewriting all the options.

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))
Related