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

2024-06-16 12:28:17 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我想通过在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():按钮被选中,它应该清除所有小部件并加载新的标签、按钮和输入框。一旦输入数据并单击submit按钮,它就应该加载defSubmitDetails():,并检索存储在输入框中名称(例如APID1)下的数据,并将它们加载到数据库中

我遇到的问题是我相信APID1.get()命令没有按它应该的方式工作,并且没有得到我以前在子例程的输入框中输入的数据。因此,它只是在文件中输入“”,而不是我输入的数据。关于如何将这些数据传递给def APSubmitDetails():子例程有什么建议吗


Tags: the数据textplacebuttonrootproductlabel
1条回答
网友
1楼 · 发布于 2024-06-16 12:28:17

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

相关问题 更多 >