Search code examples
pythonsqltkintersubmitsubroutine

Variable data not being passed to subroutines with the .get() command


So I want to add data to an SQL Database by using the SELECT command within a GUI.

main_program()

    APID1=StringVar()
    APName1=StringVar()
    APDesc1=StringVar()

    def createnewproduct(): 
            def APSubmitDetails():
                db = sqlite3.connect('File')
                cursor = db.cursor()
                ProductID = APID1.get()
                ProductName = APName1.get()
                ProductDesc = APDesc1.get()

                cursor.execute('''INSERT INTO ProductTable(ProductID, ProductName, ProductDesc)
                VALUES(:ProductID, :ProductName, :ProductDesc)''',
            {'ProductID':ProductID,'ProductName':ProductName,'ProductDesc':ProductDesc})



            #Deletes all the widgets in the screen and then recreates the title/buttons.
            for widget in pwindow.winfo_children():
                widget.destroy()



            APID=Label(pwindow, text = "Product ID: ").place(x=155, y=100)
            APIDA=Entry(pwindow, textvariable=APID1).place(x=225, y=100)
            APName=Label(pwindow, text ="Name : " ).place(x=155,y=125)
            APNameA=Entry(pwindow, textvariable=APName1).place(x=225, y=125)
            APDesc=Label(pwindow, text ="Desc : " ).place(x=155,y=150)
            ACDesc=Entry(pwindow, textvariable=APDesc1).place(x=225, y=150)
            ACSubmit=Button(pwindow, text="Submit", command= APSubmitDetails).place(x=190, y=175)


            labelspare=Label(pwindow,text ="Would you like to Query the Current Product Table or create a New Product Record?").place(x=10,y=10)
            CWbutton1=Button(pwindow,text="Current Product",command = searchforproduct).place(x=100,y=40)
            CWbutton2=Button(pwindow,text="New Product", command = createnewproduct).place(x=300,y=40)


    pwindow = Tk()
    pwindow.geometry("500x600")
    pwindow.title("Product")
    labelspare=Label(pwindow,text ="Would you like to Query the Current Product Table or create a New Product Record?").place(x=10,y=10)

    PWbutton1=Button(pwindow,text="Current Product",command = searchforproduct).place(x=100,y=40)
    PWbutton2=Button(pwindow,text="New Product", command = createnewproduct).place(x=300,y=40)

    pwindow.mainloop()

root = tk.Tk()
root.title("Apollo Blinds")
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry("1024x800")
main_program()
root.mainloop()

Firstly, it load up two buttons. Once the createnewproduct(): button is chosen, It should then clear all widgets and load up the new labels, buttons and entry boxes. Once the data has been entered into there and the submit button is clicked, It should load the defSubmitDetails(): and retrieve the data that had been stored in the entry boxes under the names e.g APID1 and load them into the database.

The problem I am having is I believe the APID1.get() command isn't working as it should and is not getting the data I entered into the entry boxes in the subroutine before. Therefore, It just enters " " into the file, not the data I had entered. Any suggestions on how to pass this data to the def APSubmitDetails(): subroutine?


Solution

  • At least part of the problem is that you are creating two instances of Tk. A tkinter application must have exactly one instance of Tk, and should call mainloop() exactly once. If you need multiple windows, create instances of Toplevel.