如何从 tkcalendar 输入数据到 SQL

How to enter data to SQL from tkcalendar

我有一个创建表单的程序,因此您可以使用它向 SQL 服务器数据库添加条目。

我想添加的其中一件事是日历小部件,这样用户就可以通过单击 tkcalendar 小部件中的日期来选择时间,而不是编写它。我创建了小部件,但我不知道如何从小部件获取日期,然后将其输入 SQL 服务器。

有人可以帮忙吗?

This is what it looks like currently.

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

    options= [
        "P",
        "V"
    ]

    clicked = StringVar()
    clicked.set("P")

    def grad_date1():
        kuupäev = Toplevel(top)
        kuupäev.geometry("270x250")
        kuupäev['bg'] = '#26658f'
        cal = Calendar(kuupäev, selectmode = 'day',
        year = 2022, month = 2,
        day = 25)
        cal.grid(row=0,column=0,padx=(10, 10), columnspan = 2)

        def grad_date():

 
            date.config(text = "Selected Date is: " + cal.get_date())
 
        # Add Button and Label
        Button(kuupäev, text = "Get Date",
       command = grad_date).grid(row=1,column=1,pady = 20)
 
        date = Label(kuupäev, text = "")
        date.grid(row=1,column=0,padx=(10, 10))



    #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= Button(top, text="Date Selection", command = grad_date1)
    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')
        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.#.#;DATABASE=###;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(), clicked.get(), Operaator_entry.get(), Pink_entry.get())
        cur1 = con1.cursor()
        cur1.execute(cur2,val)

        con1.commit()     
        con1.close()
        close_win(top)
    
    
    #Nupud
    button= Button(top, text="Salvesta", command= submit_it)
    button.grid(row=5,column=1,pady=(10, 10))
    button= Button(top, text="Tühjenda", command= delete)
    button.grid(row=5,column=3,pady=(10, 10))

    #Vormil olevate nupude,kastide ja teksti paigutus.
    top.rowconfigure(0, weight=3)
    top.rowconfigure(1, weight=3)
    top.rowconfigure(2, weight=3)
    top.rowconfigure(3, weight=3)
    top.rowconfigure(4, weight=3)
    top.rowconfigure(5, weight=3)

    top.columnconfigure(0, weight=3)
    top.columnconfigure(1, weight=3)
    top.columnconfigure(2, weight=3)
    top.columnconfigure(3, weight=3)
    top.columnconfigure(4, weight=3)

答案是用 cal.get_date()

替换 Kuupäev_entry.get()