I have a OptionMenu widget in my root window, it takes the data from sqlite3 database file When I add data to the file I need to make the OptionMenu refresh without the need to restart my the program. how to do it?
The OptionMenu code :
variable = StringVar(main_frame)
variable.set("اختار الفئة ") # default value
c.execute("SELECT category_name FROM categories")
llist = c.fetchall()
cat_option = OptionMenu(main_frame, variable, *llist)
cat_option.config(width=45, bg='#4422ee', fg='white')
cat_option.grid(row=0, column=1, sticky=S, pady=10)
The entry filed where i take the new string from :
new_categ = StringVar()
entry_cat = Entry(add_cat_frame, textvariable=new_categ)
entry_cat.config(width=50)
entry_cat.grid(row=1, column=1)
and here is the function that should change :
def adding_new_cat():
nc = new_categ.get()
c.execute("CREATE TABLE IF NOT EXISTS categories(id INTEGER PRIMARY KEY AUTOINCREMENT, "
"category_name TEXT NOT NULL )")
c.execute("INSERT INTO categories (category_name) VALUES (?)", (nc,))
conn.commit()
What happens that it really changes but I need to reopen my program to see changes appears ( the new item in the optionmenu ) , before restarting it shows the old list of values without the new added one
Tried to add-command and used this code :
# Reset var and delete all old options
variable.set('')
cat_option['menu'].delete(0, 'end')
# Insert list of new options (tk._setit hooks them up to var)
new_choices = llist # the list from db
for choice in new_choices:
cat_option['menu'].add_command(label=choice, command=Tk._setit(variable, choice))
and still, it gives me an error about the _setit att.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "C:/python/warehouse/main.py", line 328, in exit_add_cat
cat_option['menu'].add_command(label=choice, command=Tk._setit(variable,
choice))
AttributeError: class Tk has no attribute '_setit'
Please note that I tried using Tk._setit
and Tkinter._setit
and it still gives me the same error
SOLVED
by redoing the select query and adding it to the button that should add or modify values in the optionmenu
Added this code to the button
c.execute("SELECT category_name FROM categories")
llist = c.fetchall()
cat_option = OptionMenu(main_frame, variable, *llist)
cat_option.config(width=45, bg='#4422ee', fg='white')
cat_option.grid(row=0, column=1, sticky=S, pady=10)