总日期问题

时间:2020-11-12 22:16:55

标签: python

一切正常,但是单击“添加/总计”按钮进行“总天数”的计算时,出现以下错误 TotalDays.set(abs(OutDate-InDate.days)) AttributeError:“ str”对象没有属性“ days”

from tkinter import*

从tkinter导入ttk 随机导入 导入tkinter.messagebox 从datetime导入datetime 导入数据库酒店 导入日期时间 导入时间 从datetime导入datetime,timedelta 从tkinter导入菜单

#Frontend

类酒店: def init (自身,root): self.root =根 self.root.title('Elime Guest Inn') self.root.geometry('1350x595 + 0 + 0')

    MainFrame = Frame(self.root)
    MainFrame.grid()
    TopFrame = Frame(MainFrame, bd=10, width=1350, height=550, padx=2, bg='gold', relief=RIDGE)
    TopFrame.pack(side=TOP)

    LeftFrame = Frame(TopFrame, bd=10, width=400, height=550,bg='white', relief=RIDGE)
    LeftFrame.pack(side=LEFT)

    RightFrame = Frame(TopFrame, bd=10, width=820, height=550, relief=RIDGE)
    RightFrame.pack(side=RIGHT)
    RightFrame1 = Frame(RightFrame, bd=5, width=900, height=50, padx=10,bg='gold', relief=RIDGE)
    RightFrame1.grid(row=0,column=0)
    RightFrame2 = Frame(RightFrame, bd=5, width=800, height=100, padx=10, bg='white', relief=RIDGE)
    RightFrame2.grid(row=1,column=0)
    RightFrame3 = Frame(RightFrame, bd=5, width=800, height=100, padx=10, bg='white', relief=RIDGE)
    RightFrame3.grid(row=3,column=0)

    BottomFrame = Frame(MainFrame, bd=10, width=1350, height=100, padx=2, bg='gold', relief=RIDGE)
    BottomFrame.pack(side=BOTTOM)

    #VARIABLES
    global hd
    CusID =StringVar()
    Fullname=StringVar()
    Address =StringVar()
    Phone =StringVar()
    Email =StringVar()
    IDNo =StringVar()
    IDType =StringVar()
    Gender =StringVar()
    Nationality =StringVar()
    Occupation =StringVar()
    Reason =StringVar()
    DateIn =StringVar()
    DateOut =StringVar()
    RoomType =StringVar()
    RoomNo =StringVar()
    Price =StringVar()
    TotalDays =StringVar()
    ExchangeRate =StringVar()
    SubTotal =StringVar()
    Total =StringVar()

    DateIn.set(time.strftime('%d/%m/%Y'))
    DateOut.set(time.strftime('%d/%m/%Y'))

    x = random.randint(1,  8700)
    randomRef = str(x)
    CusID.set('Elim' + randomRef)

    

    def iExit():
        iExit = tkinter.messagebox.askyesno('Elim Guest Inn System', 'Confirm Exit')
        if iExit > 0:
            root.destroy()
            return

    def Reset():

        IDType.set(' ')
        Gender.set(' ')
        Nationality.set(' ')
        RoomType.set(' ')
        RoomNo.set(' ')
        Price.set(' ')
        self.txtCusID.delete(0,END)
        self.txtFullName.delete(0,END)
        self.txtAddress.delete(0,END)
        self.txtPhoneNo.delete(0,END)
        self.txtEmail.delete(0,END)
        self.txtIDNo.delete(0,END)
        self.txtOccupation.delete(0,END)
        self.txtReason.delete(0,END)
        self.txtDateIn.delete(0,END)
        self.txtDateOut.delete(0,END)
        self.txtTotalDays.delete(0,END)
        self.txtExchangeRate(0,END)
        self.txtSubTotal.delete(0,END)
        self.txtTotal(0,END)

        DateIn.set(time.strftime('%d/%m/%Y'))
        DateOut.set(time.strftime('%d/%m/%Y'))

        x = random.randint(1,  8700)
        randomRef = str(x)
        CusID.set('Elim' + randomRef)


    def addData():
        if(len(CusId.get())!=(0)):
           databasehotel.addHotelrec(CusRef.get(), Fullname.get(), Address,get(), Phone.get(), Email.get(), IDNo.get(), IDType.get(), Gender.get(), Nationality.get(),
                                      Occupation.get(), Reason.get(), CheckinDate.get(), CheckOutDate.get(), RoomType.get(), RoomNo.get(), Price.get())
           lstHotel.delete(0,END)
           lstHotel.insert(END,(CusRef.get(), Fullname.get(), Address,get(), Phone.get(), Email.get(), IDNo.get(), IDType.get(), Gender.get(), Nationality.get(),
                                      Occupation.get(), Reason.get(), CheckinDate.get(), CheckOutDate.get(), RoomType.get(), RoomNo.get(), Price.get()))
                           
        
    def displayData():
        lstHotel.delete(0,END)
        for row in Databasehotel.viewData():
            lstHotel.insert(END,row,str(''))

    def HotelRec():
        global hd
        searchHdb = lstHOtel.curselction()[0]
        hd = lstHotel.get(searchHdb)
        
        self.txtCusID.delete(0,END)
        self.txtCusID.Insert(0,END[1])
        
        self.txtFullName.delete(0,END)
        self.txtFullName.insert(0,END[2])
        
        self.txtAddress.delete(0,END)
        self.txtAddress.insert(0,END[3])
        
        self.txtPhoneNo.delete(0,END)
        self.txtPhoneNo.insert(0,END[12])
        #self.txtEmail.delete(0,END)
        #self.txtEmail.insert(0,END)
        self.txtIDNo.delete(0,END)
        self.txtIDNo.insert(0,END[4])
        self.txtIDType.delete(0,END)                  
        self.txtIDType.insert(0,END[5])
        self.txtNationality.delete(0,END)                    
        self.txtNationality.insert(0,END[6])
        self.textGender.delete(0,END)                    
        self.txtGender.insert(0,END[7])
        self.txtOccupation.delete(0,END)                                
        self.txtOccupation.insert(0,END[8])
        #self.txtreason.delete(0,END)
        #self.txtReason.insert(0,END)
        self.txtDateIn.delete(0,END)
        self.txtDateIn.insert(0,END[10])
        self.txtDateOut.delete(0,END)
        self.txtDateOut.insert(0,END[11])
        self.textRoomNo.delete(0,END)
        self.textRoomNo.insert(0,END[9])
        #self.txtdays.delete(0,END)
        #self.txtExchangeRate(0,END)
        #self.txtSubTotal.delete(0,END)
        #self.txtTotal(0,END)

    def DeleteData():
        if(len(CusID.get())!=0):
            Databasehotel.deleteRec(hd[0])
            Reset()
            DisplayData()

    def searchDatabase():
        lsHotel.delete(0,END)
        for row in DAtabasehotel.searchData(CusRef.get(), Fullname.get(), Address,get(), Phone.get(), Email.get(), IDNo.get(), IDType.get(), Gender.get(), \
            Nationality.get(), Occupation.get(), Reason.get(), CheckinDate.get(), CheckOutDate.get(), RoomType.get(), RoomNo.get(), Price.get()):
            lstHotel.insert(END,row,str(''))

    def update():
        if(len(CusId.get())!=0):
            Databasehotel.deleteRec(hd[0])
        if(len(CusId.get())!=0):
            Databasehotel.addStdRec(CusRef.get(), Fullname.get(), Address,get(), Phone.get(), Email.get(), IDNo.get(), IDType.get(), Gender.get(), \
                Nationality.get(), Occupation.get(), Reason.get(), CheckinDate.get(), CheckOutDate.get(), RoomType.get(), RoomNo.get(), Price.get())
            lstHotel.delete(0,END)
            lstHotel.insert(END,(CusRef.get(), Fullname.get(), Address,get(), Phone.get(), Email.get(), IDNo.get(), IDType.get(), Gender.get(), \
                Nationality.get(), Occupation.get(), Reason.get(), CheckinDate.get(), CheckOutDate.get(), RoomType.get(), RoomNo.get(), Price.get()))

    def TotalCostandAddData():
        InDate = DateIn.get()
        OutDate = DateOut.get()
        indate = datetime.strptime(InDate,'%d/%m/%Y')
        outdate = datetime.strptime(OutDate,'%d/%m/%Y')
        TotalDays.set(abs((OutDate - InDate).days))

#===============标签 self.lblCusID = Label(LeftFrame,font =('arial',12,'bold'),bg ='white',text ='Customer Ref:',padx = 1) self.lblCusID.grid(row = 0,column = 0,stick = W) self.txtCusID = Entry(LeftFrame,font =('arial',12,'bold'),width = 18,textvariable = CusID) self.txtCusID.grid(row = 0,column = 1,stick = W)

    self.lblFullName=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Full Name:',padx=1)
    self.lblFullName.grid(row=1,column=0, stick =W)
    self.txtFullName=Entry(LeftFrame, font=('arial',12,'bold'),width=18)
    self.txtFullName.grid(row=1,column=1, stick =W)

    self.lblAddress=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Address:',padx=1)
    self.lblAddress.grid(row=2,column=0, stick =W)
    self.txtAddress=Entry(LeftFrame, font=('arial',12,'bold'),width=18, textvariable=Address)
    self.txtAddress.grid(row=2,column=1, stick =W)

    self.lblPhoneNo=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Phone No.:',padx=1)
    self.lblPhoneNo.grid(row=3,column=0, stick =W)
    self.txtPhoneNo=Entry(LeftFrame, font=('arial',12,'bold'),width=18,)
    self.txtPhoneNo.grid(row=3,column=1, stick =W)

    self.lblEmail=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Email:',padx=1)
    self.lblEmail.grid(row=4,column=0, stick =W)
    self.txtEmail=Entry(LeftFrame, font=('arial',12,'bold'),width=18,)
    self.txtEmail.grid(row=4,column=1, stick =W)

    self.lblIDNo=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='ID No.:',padx=1)
    self.lblIDNo.grid(row=5,column=0, stick =W)
    self.txtIDNo=Entry(LeftFrame, font=('arial',12,'bold'),width=18, textvariable=IDNo)
    self.txtIDNo.grid(row=5,column=1, stick =W)

    self.lblIDType=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='ID Type:',padx=1)
    self.lblIDType.grid(row=6,column=0, stick =W)
    self.cboIDType = ttk.Combobox(LeftFrame, textvariable=IDType, state='readonly', font=('arial',12,'bold'),width=16)
    self.cboIDType['value'] = (' ', 'Passport', 'Pilot Licence', 'Drivers Licence', 'ID')
    self.cboIDType.current(0)
    self.cboIDType.grid(row=6,column=1, stick =W)

    self.lblGender=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Gender:',padx=1)
    self.lblGender.grid(row=7,column=0, stick =W)
    self.cboGender = ttk.Combobox(LeftFrame, textvariable=Gender, state='readonly', font=('arial',12,'bold'),width=16)
    self.cboGender['value'] = (' ', 'Male', 'Female')
    self.cboGender.current(0)
    self.cboGender.grid(row=7,column=1, stick =W)

    self.lblNationality=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Nationality:',padx=1)
    self.lblNationality.grid(row=8,column=0, stick =W)
    self.cboNationality = ttk.Combobox(LeftFrame, textvariable=Nationality, state='readonly', font=('arial',12,'bold'),width=16)
    self.cboNationality['value'] = (' ', 'Male', 'Female')
    self.cboNationality.current(0)
    self.cboNationality.grid(row=8,column=1, stick =W)

    self.lblOccupation=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Occupation:',padx=1)
    self.lblOccupation.grid(row=9,column=0, stick =W)
    self.txtOccupation=Entry(LeftFrame, font=('arial',12,'bold'),width=18, textvariable=Occupation)
    self.txtOccupation.grid(row=9,column=1, stick =W)

       
    self.lblReason=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Reason:',padx=1)
    self.lblReason.grid(row=10,column=0, stick =W)
    self.txtReason=Entry(LeftFrame, font=('arial',12,'bold'),width=18, textvariable=Reason)
    self.txtReason.grid(row=10,column=1, stick =W)

    #self.lblRoomNo=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Room No.:',padx=1)
    #self.lblRoomNo.grid(row=11,column=0, stick =W)
    #self.txtRoomNo=Entry(LeftFrame, font=('arial',12,'bold'),width=18)
    #self.txtRoomNo.grid(row=11,column=1, stick =W)

    self.lblDateIn=Label(LeftFrame, font=('arial',12,'bold'),bg='white',text='Check In Date:',padx=1)
    self.lblDateIn.grid(row=12,column=0, stick =W)
    self.txtDateIn=Entry(LeftFrame, font=('arial',12,'bold'),width=18, textvariable=DateIn)
    self.txtDateIn.grid(row=12,column=1, stick =W)

    self.lblDateOut=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Check Out Date:',padx=1)
    self.lblDateOut.grid(row=13,column=0, stick =W)
    self.txtDateOut=Entry(LeftFrame, font=('arial',12,'bold'),width=18, textvariable=DateOut)
    self.txtDateOut.grid(row=13,column=1, stick =W)

    self.lblRoomType=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Room Type:',padx=1)
    self.lblRoomType.grid(row=14,column=0, stick =W)
    self.cboRoomType = ttk.Combobox(LeftFrame, textvariable=RoomType, state='readonly', font=('arial',12,'bold'),width=16)
    self.cboRoomType['value'] = (' ', 'Queen Bed', 'Queen Balcony', 'Double')
    self.cboRoomType.current(0)
    self.cboRoomType.grid(row=14,column=1, stick =W)

    self.lblRoomNo=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Room No.:',padx=1)
    self.lblRoomNo.grid(row=15,column=0, stick =W)
    self.cboRoomNo = ttk.Combobox(LeftFrame, textvariable=RoomNo, state='readonly', font=('arial',12,'bold'),width=16)
    self.cboRoomNo['value'] = (' ','Second Floor ','201','202','203','204','205','206','207','208',
                               'Third Floor ','301','302','303','304','305','306','307','308',
                               'Fourt Floor ','401','402','403','404','405','406','407','408')
    self.cboRoomNo.current(0)
    self.cboRoomNo.grid(row=15,column=1, stick =W)

    self.lblPrice=Label(LeftFrame, font=('arial',12,'bold'),bg='white', text='Price:',padx=1)
    self.lblPrice.grid(row=16,column=0, stick =W)
    self.cboPrice = ttk.Combobox(LeftFrame, textvariable=Price, state='readonly', font=('arial',12,'bold'),width=16)
    self.cboPrice['value'] = (' ', '35', '40', '50')
    self.cboPrice.current(0)
    self.cboPrice.grid(row=16,column=1, stick =W)

#Top Label Positon ======================

    self.lblTopLabel=Label(RightFrame1, font=('arial',8,'bold'),bg='white',padx=6,pady=10,
                           text='Customer Id\tFullName\tAddress\tIDNo\tIDType\tNationality\tGender\tOccupation\tRoomNo\tCheckInDate\tCheckOutDate\tPhoneNo')
    self.lblTopLabel.grid(row=0,column=0, columnspan=17)

    scrollbar = Scrollbar(RightFrame2)
    scrollbar.grid(row=0,column=0,sticky='ns')
    lstHotel = Listbox(RightFrame2, width=103, height=14,font=('arial',12,'bold'), yscrollcommand=scrollbar.set)
                       
    lstHotel.bind('<<ListboxSelect>>')
    lstHotel.grid(row=0, column=0, padx=7, sticky='nsew')
    scrollbar.config(command = lstHotel.xview)

#Labelforrpices ==================================

    self.lblTotalDays=Label(RightFrame3, font=('arial',12,'bold'),bg='white',text='Days:',padx=1)
    self.lblTotalDays.grid(row=0,column=0, stick =W)
    self.txtTotalDays=Entry(RightFrame3, font=('arial',12,'bold'),width=76,textvariable=TotalDays)
    self.txtTotalDays.grid(row=0,column=1, stick =W)

    self.lblExchangeRate=Label(RightFrame3, font=('arial',12,'bold'),bg='white', text='Exchange Rate.:',padx=1)
    self.lblExchangeRate.grid(row=1,column=0, stick =W)
    self.txtExchangeRate=Entry(RightFrame3, font=('arial',12,'bold'),width=76, textvariable=ExchangeRate)
    self.txtExchangeRate.grid(row=1,column=1, stick =W)

    self.lblSubTotal=Label(RightFrame3, font=('arial',12,'bold'),bg='white', text='Sub Total.:',padx=1)
    self.lblSubTotal.grid(row=2,column=0, stick =W)
    self.txtSubTotal=Entry(RightFrame3, font=('arial',12,'bold'),width=76, textvariable=SubTotal)
    self.txtSubTotal.grid(row=2,column=1, stick =W)

    self.lblTotal=Label(RightFrame3, font=('arial',12,'bold'),bg='white', text='Total:',padx=1)
    self.lblTotal.grid(row=3,column=0, stick =W)
    self.txtTotal=Entry(RightFrame3, font=('arial',12,'bold'),width=76, textvariable=Total)
    self.txtTotal.grid(row=3,column=1, stick =W)

    

#Buttons ==================================

    self.btnTotalandAddData1 = Button(BottomFrame, pady=1, bd=4, font=('arial',12,'bold'),
                     width=13, height=2, text='Add New Total', command=TotalCostandAddData).grid(row=0,column=0, padx=4)
    
    self.btnDisplay = Button(BottomFrame, pady=1, bd=4, font=('arial',12,'bold'),
                     width=13, height=2, text='Display').grid(row=0,column=1, padx=4)
    
    self.btnUpdate = Button(BottomFrame, pady=1, bd=4, font=('arial',12,'bold'),
                     width=13, height=2, text='Update').grid(row=0,column=2, padx=4)
    
    self.btnDelete = Button(BottomFrame, pady=1, bd=4, font=('arial',12,'bold'),
                     width=13, height=2, text='Delete').grid(row=0,column=3, padx=4)

    self.btnSearch = Button(BottomFrame, pady=1, bd=4, font=('arial',12,'bold'),
                     width=13, height=2, text='Search').grid(row=0,column=4, padx=4)

    self.btnReset = Button(BottomFrame, pady=1, bd=4, font=('arial',12,'bold'),
                     width=13, height=2, text='Reset', command = Reset).grid(row=0,column=5, padx=4)

    self.btnExit = Button(BottomFrame, pady=1, bd=4, font=('arial',12,'bold'),
                     width=13, height=2, text='Exit', command = iExit).grid(row=0,column=6, padx=4)
    
    
               


if __name__=='__main__':
root = Tk()
application = Hotel (root)
root.mainloop()
                    

1 个答案:

答案 0 :(得分:0)

第一个InDate是一个字符串,没有属性天。我认为您想使用indate.days(请记住python区分大小写)。其次,如果您希望以天为单位的增量时间,则应将.days属性用于间隔而不是indate。将最后一行替换为:

TotalDays.set(abs((outdate - indate).days))