Home > Software engineering >  How to insert data from drop down menu to SQL
How to insert data from drop down menu to SQL

Time:02-26

I need to make a couple of dropdown menus, and then insert the data into a SQL database. But it doesn't let me take data from the dropdown menu like it does with entries, how can I get and add data from the dropdown menu to my sql database? Currently it gives the error "AttributeError: 'OptionMenu' object has no attribute 'get'" I tried to remove .get() from the end of it, but that didn't help.

Here is my code:


def vorm():
    #Aken
    top= Toplevel(ws)
    top.geometry("720x200")
    top['bg'] = '#26658f'

    options= [
        "P",
        "V"
    ]

    clicked = StringVar()
    clicked.set("P")
    
    #Tekst
    Kuupäev = Label(top,text="Kuupäev", background="#26658f")
    Kuupäev.grid(row=0,column=0,padx=(10, 10))
    Tellimus= Label(top,text="Tellimus", background="#26658f")
    Tellimus.grid(row=0,column=1,padx=(10, 10))
    Pos = Label(top,text="Pos", background="#26658f")
    Pos.grid(row=0,column=2,padx=(10, 10))
    Klient = Label(top,text="Klient", background="#26658f")
    Klient.grid(row=0,column=3,padx=(10, 10))
    Toode = Label(top,text="Toode", background="#26658f")
    Toode.grid(row=0,column=4,padx=(10, 10))
    Operatsioon = Label(top,text="Operatsioon", background="#26658f")
    Operatsioon.grid(row=2,column=0,padx=(10, 10))
    Kogus = Label(top,text="Kogus", background="#26658f")
    Kogus.grid(row=2,column=1,padx=(10, 10))
    Käsi = Label(top,text="Käsi", background="#26658f")
    Käsi.grid(row=2,column=2,padx=(10, 10))
    Operaator = Label(top,text="Operaator", background="#26658f")
    Operaator.grid(row=2,column=3,padx=(10, 10))
    Pink = Label(top,text="Pink", background="#26658f")
    Pink.grid(row=2,column=4,padx=(10, 10))
    #Kirjakastid
    Kuupäev_entry = Entry(top)
    Kuupäev_entry.grid(row=1,column=0,padx=(10, 10))
    Tellimus_entry = Entry(top)
    Tellimus_entry.grid(row=1,column=1,padx=(10, 10))
    Pos_entry = Entry(top)
    Pos_entry.grid(row=1,column=2,padx=(10, 10))
    Klient_entry = Entry(top)
    Klient_entry.grid(row=1,column=3,padx=(10, 10))
    Toode_entry = Entry(top)
    Toode_entry.grid(row=1,column=4,padx=(10, 10))
    Operatsioon_entry = Entry(top)
    Operatsioon_entry.grid(row=3,column=0,padx=(10, 10))
    Kogus_entry = Entry(top)
    Kogus_entry.grid(row=3,column=1,padx=(10, 10))
    Käsi_entry = OptionMenu(top, clicked, *options)
    Käsi_entry.grid(row=3,column=2,padx=(10, 10))
    Operaator_entry = Entry(top)
    Operaator_entry.grid(row=3,column=3,padx=(10, 10))
    Pink_entry = Entry(top)
    Pink_entry.grid(row=3,column=4,padx=(10, 10))

    #"Tühjenda"
    def delete():
        Kuupäev_entry.delete(0, 'end')
        Tellimus_entry.delete(0, 'end')
        Pos_entry.delete(0, 'end')
        Klient_entry.delete(0, 'end')
        Toode_entry.delete(0, 'end')
        Operatsioon_entry.delete(0, 'end')
        Kogus_entry.delete(0, 'end')
        Käsi_entry.delete(0, 'end')
        Operaator_entry.delete(0, 'end')
        Pink_entry.delete(0, 'end') 

    #"Salvesta" 
    def submit_it():
        con1 = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=192.168.1.8;DATABASE=HUT;UID=######;PWD=######')
    
        cur2 = "insert into PROD_MachiningEventsTEST ([Date], [Order], [Position], [Client], [Product], [Operation], [Amount], [OpeningSide], [Operator], [Machine]) values(?,?,?,?,?,?,?,?,?,?)"
        val = (Kuupäev_entry.get(), Tellimus_entry.get(), Pos_entry.get(), Klient_entry.get(), Toode_entry.get(), Operatsioon_entry.get(), Kogus_entry.get(), Käsi_entry.get(), Operaator_entry.get(), Pink_entry.get())
        cur1 = con1.cursor()
        cur1.execute(cur2,val)

        con1.commit()     
        con1.close()
        close_win(top)

CodePudding user response:

You need to use the tkinter variable (clicked) linked to the OptionMenu (Käsi_entry) instead:

val = (Kuupäev_entry.get(), Tellimus_entry.get(), Pos_entry.get(),
       Klient_entry.get(), Toode_entry.get(), Operatsioon_entry.get(),
       Kogus_entry.get(), clicked.get(), Operaator_entry.get(), # used clicked instead of Käsi_entry
       Pink_entry.get())
  • Related