0

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)
Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116
  • You need the change the data in it, or you've changed the data and just need to get it to refresh its display? – abarnert Mar 14 '18 at 21:54
  • Or do you know how to do both of those, but you don't know how to trigger them to happen automatically when you add data to the database? – abarnert Mar 14 '18 at 21:55
  • yes I do both of them, but I have to restart the program to see the optionmenu updated with the new data, I need to refresh its display, it is already being updated when i add data to db , but need to restart the GUI to see it changed for user – Ahmed Wagdi Mar 14 '18 at 22:11
  • That response seems to be contradictory. You say you refresh its display, and then that you need to refresh its display. Can you just give us a [minimal, complete, verifiable example](https://stackoverflow.com/help/mcve) of code that demonstrates the problem, instead of trying to explain it? Also, you should edit your question to have all the information needed to answer it, not just reply in comments. People won't see the comments from the feed, search indexes won't see them either, etc. – abarnert Mar 14 '18 at 22:15
  • Sorry for not adding enough information , i think it is better now – Ahmed Wagdi Mar 14 '18 at 22:32
  • OK, so you're _not_ changing the data in the option menu, and that's the part you need to know how to do. You can either manually `add_command` the new value to the option menu (assuming you want it to go to the end, or some other location you can specify), or you can clear it out, redo the select query, and add each value. So I think this is ultimately a dup of [this question](https://stackoverflow.com/questions/17580218/changing-the-options-of-a-optionmenu-when-clicking-a-button). But if that doesn't help you, explain what I'm missing. – abarnert Mar 14 '18 at 22:42
  • @abarnert I couldn't manually `add_command` it still returns an error...info updated on the question itself – Ahmed Wagdi Mar 15 '18 at 09:01
  • redoing the select query worked. thank man :) – Ahmed Wagdi Mar 15 '18 at 16:12

0 Answers0