变量数据未使用 .get() 命令传递到子例程



所以我想通过在 GUI 中使用 SELECT 命令将数据添加到 SQL 数据库。

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

首先,它加载了两个按钮。选择createnewproduct():按钮后,它应该清除所有小部件并加载新的标签、按钮和输入框。一旦数据被输入到那里并单击提交按钮,它应该加载defSubmitDetails():并检索存储在名称下的输入框中的数据,例如APID1并将它们加载到数据库中。

我遇到的问题是我相信 APID1.get() 命令没有正常工作,并且没有获取我之前在子例程的输入框中输入的数据。因此,它只是在文件中输入" ",而不是我输入的数据。关于如何将此数据传递给def APSubmitDetails():子例程的任何建议?

至少部分问题是您正在创建两个Tk实例。一个 tkinter 应用程序必须只有一个 Tk 实例,并且应该只调用mainloop()一次。如果需要多个窗口,请创建 Toplevel 的实例。

最新更新