0

I have been running into an issue with my company's Python program.

I am designing a program that will allow us to make a pdf straight from the program, and find the distance between 2 points (which is placed in the program in the form of 2 addresses). How can I solve the following error?

 Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\micae\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1895, in __call__
    return self.func(*args)
  File "C:/Users/micae/AppData/Roaming/JetBrains/PyCharmCE2020.3/scratches/scratch.py", line 271, in invoice_calculate
    Button(calculate_window,text="DONE!",bg="white",fg="red",height=3,width=40,command=finalize(prerpkbrkdn,prerpktotal,rpkout,spec_req,outdistance)).grid(row=14,column=3,sticky=W)
  File "C:/Users/micae/AppData/Roaming/JetBrains/PyCharmCE2020.3/scratches/scratch.py", line 231, in finalize
    pdf.page2(distance_ab, "50.02 hours", total)
  File "C:/Users/micae/AppData/Roaming/JetBrains/PyCharmCE2020.3/scratches/scratch.py", line 86, in page2
    self.cell(w=25.273, h=3.276, align='L', txt=distance, border=0)
  File "C:\Users\micae\AppData\Local\Programs\Python\Python38\lib\site-packages\fpdf\fpdf.py", line 150, in wrapper
    return fn(self, *args, **kwargs)
  File "C:\Users\micae\AppData\Local\Programs\Python\Python38\lib\site-packages\fpdf\fpdf.py", line 755, in cell
    txt2 = self._escape(txt)
  File "C:\Users\micae\AppData\Local\Programs\Python\Python38\lib\site-packages\fpdf\fpdf.py", line 1899, in _escape
    return s.replace('\\','\\\\').replace(')','\\)').replace('(','\\(').replace('\r','\\r')
AttributeError: 'float' object has no attribute 'replace'

The code is as follows:

 from tkinter import *
import requests
from fpdf import FPDF

######modules#####

class PDF(FPDF):
    def lines(self):
        pass
        #pdf_h = 297.0
        #pdf_w = 210.0
        #self.set_line_width(0.0)
        #self.line(10,pdf_h/11,pdf_w-10,pdf_h/11)

    def page1(self,Qoutenum,Date,name,email,comp_address,comp_tel,MTY,pickup_date,pickup_time,Dropoff_time,dropoff_date,Special_requirements,km,cost):
        pdf_h = 297.0
        pdf_w = 210.0
        self.set_xy(29,80)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0,0,0)
        self.cell(w=23.914, h=4.552, align='C', txt=Date, border=0)

        self.set_xy(159, 80)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=23.914, h=4.552, align='C', txt=Qoutenum, border=0)

        self.set_xy(10, 85)
        self.set_font('Arial', 'B', 10)
        self.set_text_color(0, 0, 0)
        self.cell(w=50, h=35.616, align='C', txt=name, border=0)

        self.set_xy(19, 90)
        self.set_font('Arial', 'B', 10)
        self.set_text_color(0, 0, 0)
        self.cell(w=50, h=35.616, align='C', txt=comp_address, border=0)

        self.set_xy(12, 95)
        self.set_font('Arial', 'B', 10)
        self.set_text_color(0, 0, 0)
        self.cell(w=50, h=35.616, align='C', txt=email, border=0)

        self.set_xy(6, 100)
        self.set_font('Arial', 'B', 10)
        self.set_text_color(0, 0, 0)
        self.cell(w=50, h=35.616, align='C', txt=comp_tel, border=0)



        self.set_xy(59.261, 202)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=pickup_date, border=0)

        self.set_xy(163.495, 202)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=pickup_time, border=0)

        self.set_xy(59.261, 213)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=dropoff_date, border=0)

        self.set_xy(163.495, 213)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=Dropoff_time, border=0)

        self.set_xy(15, 230)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=Special_requirements, border=0)

        self.set_xy(47.769, 266)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=MTY, border=0)



    def page2(self,distance,traveltime,total):
        self.set_xy(41.029, 11)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=distance, border=0)

        self.set_xy(159.902, 11)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=traveltime, border=0)

        self.set_xy(29.217, 87)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=total, border=0)


    def imagex(self,pic):
        self.set_xy(1, 2)
        self.image(pic, link='', type='', w=205, h=270)


    def invoice(self,invoice):
        count=0
        start_co=28.203
        text=""
        while count !=len(invoice):
            if invoice[count] == ".":
                self.set_xy(10.404, start_co)
                self.set_font('Arial', 'B', 10)
                self.set_text_color(0, 0, 0)
                self.cell(w=185.762, h=2.972, align='L', txt=text, border=0)
                start_co=start_co+5.1
                text=""

            text=text+invoice[count]
            count=count+1

    def paragrapgh(self,input,x,start_y):
        count = 0
        start_co=start_y
        text = ""
        while count != len(input):
            if input[count] == "," or count==len(input):
                self.set_xy(x, start_co)
                self.set_font('Arial', 'B', 10)
                self.set_text_color(0, 0, 0)
                self.cell(w=185.762, h=2.972, align='L', txt=text, border=0)
                start_co = start_co + 5.1
                text = ""

            text = text + input[count]
            count = count + 1

def getco_ordinates(address):
    url = "https://trueway-geocoding.p.rapidapi.com/Geocode"

    querystring = {"address":address,"language":"en"}

    headers = {
        'x-rapidapi-key': "18cffff83fmshba0773be7ecca01p19c62cjsnfb676eb529bf",
        'x-rapidapi-host': "trueway-geocoding.p.rapidapi.com"
        }

    response = requests.request("GET", url, headers=headers, params=querystring)
    sent=response.text
    returned=findco_ordinates(sent)
    return returned

def findco_ordinates(test):
    x=test.index("location")

    count=x
    word=""
    while test[count] != "}":
        word=word+test[count]
        count+=1
    longcount=word.index(" ",55)-1
    latcount=word.index(",",25)-1
    co_ordinate=""

    for count in range(27,latcount):
        co_ordinate=co_ordinate+word[count]
    co_ordinate=co_ordinate+","

    for count in range(55,longcount):
        co_ordinate=co_ordinate+word[count]

    return co_ordinate

def co_ordinates_to_distance(pickup,dropoff):
    url = "https://trueway-matrix.p.rapidapi.com/CalculateDrivingMatrix"

    querystring = {"origins":pickup,"destinations":dropoff}

    headers = {
    'x-rapidapi-key': "18cffff83fmshba0773be7ecca01p19c62cjsnfb676eb529bf",
    'x-rapidapi-host': "trueway-matrix.p.rapidapi.com"
    }

    response = requests.request("GET", url, headers=headers, params=querystring)

    print(response.text)
    x=response.text
    count=31
    tester=x[count]
    distance=""
    while tester != " ":
        distance=distance+x[count]
        count=count+1
        tester=x[count]
    final=int(distance)/1000
    return final

def calculate_distance():
    pickup=pickup_address.get("1.0",'end-1c')
    dropoff=dropoff_address.get("1.0",'end-1c')
    origin=getco_ordinates(pickup)
    destination=getco_ordinates(dropoff)
    distance=co_ordinates_to_distance(origin,destination)
    return distance

def finalize(invoice_breakdown,pre_total,rpk,specialrequirements,distance_ab):
    namer=name.get()
    email=email_address.get()
    tel=tel_nr.get()
    mty=MTY.get()
    pickupadd=pickup_address.get("1.0",'end-1c')
    dropoffadd=dropoff_address.get("1.0",'end-1c')
    picktime=pickup_time.get()
    dropdate=dropoff_date.get()
    droptime=""
    special=specialrequirements

    ###workingss###
    total=pre_total+(rpk*int(distance_ab))



    ###pdf###
    pdf = PDF()
    pdf.add_page()
    pdf.page1("101","02/02/14",namer,email,"",tel,mty,pickupadd,picktime,droptime,dropdate,special,distance_ab,total)
    pdf.imagex("jsm invoice.png")
    pdf.paragrapgh(pickupadd, 19.247, 153)
    pdf.paragrapgh(dropoffadd, 110.118, 153)
    pdf.add_page()
    pdf.imagex("jsm invoices2.png")
    pdf.invoice(specialrequirements)
    pdf.page2(distance_ab, "50.02 hours", total)
    pdf.output("test.pdf", "F")

def invoice_calculate():
    calculate_window=Tk()
    calculate_window.title("Invoice Calculation")
    calculate_window.geometry('1500x800')
    Label(calculate_window,text="").grid(row=0,column=0)

    Label(calculate_window,text="Invoice",bg="white",fg="black",font="none 12 bold").grid(row=1,column=0)
    Label(calculate_window,text="").grid(row=2,column=0)
    Label(calculate_window,text="Pre-R.P.K Breakdown (Use . to represent a new line)",bg="white",fg="black",font="none 10 bold").grid(row=3,column=2,sticky=W)
    Pre_rpk_breakdown=Text(calculate_window,bg="white",fg="black",width=40,height=10)
    Pre_rpk_breakdown.grid(row=4,column=3)
    prerpkbrkdn=Pre_rpk_breakdown.get("1.0",'end-1c')

    Label(calculate_window, text="").grid(row=5, column=0)
    Label(calculate_window,text="Pre-R.P.K. Total:",bg="white",fg="black",font="none 10 bold").grid(row=6,column=2,sticky=W)
    Pre_rpk_total=Entry(calculate_window,bg="white",fg="black",width=60)
    Pre_rpk_total.grid(row=6,column=3)
    prerpktotal=Pre_rpk_total.get()

    Label(calculate_window,text="").grid(row=7,column=0)
    Label(calculate_window,text="Final Total (WITH R.P.K.)",bg="white",fg="black",font="none 12 bold").grid(row=8,column=0,sticky=W)

    distance=(f"{calculate_distance()}KM Total Distance     At(R.P.K.):")
    outdistance=calculate_distance()
    Label(calculate_window,text="").grid(row=9,column=0)
    Label(calculate_window,text=distance,bg="white",fg="black",font="none 10 bold").grid(row=10,column=2,sticky=W)
    rpk=Entry(calculate_window,bg="white",fg="black",width=10)
    rpk.grid(row=10,column=3,sticky=W)
    rpkout=rpk.get()

    Label(calculate_window,text="").grid(row=11,column=0)
    Label(calculate_window,text="Special Requirements",bg="white",fg="black",font="none 10 bold").grid(row=12,column=2,sticky=W)
    special_requirements=Text(calculate_window,bg="white",fg="black",width=40,height=10)
    special_requirements.grid(row=12,column=3,sticky=W)
    spec_req=special_requirements.get("1.0",'end-1c')

    Label(calculate_window,text="").grid(row=13,column=0)
    Button(calculate_window,text="DONE!",bg="white",fg="red",height=3,width=40,command=finalize(prerpkbrkdn,prerpktotal,rpkout,spec_req,outdistance)).grid(row=14,column=3,sticky=W)


    calculate_window.mainloop()

window = Tk()

window.title("JSM_SOLUTIONS_INTERNAL_PROGRAM")

window.geometry('1500x800')

Label(window,text="",bg="white",fg="black",font="none 12 bold") .grid(row=1,column=0,sticky=W)

Label(window,text="Client Info:",bg="white",fg="black",font="none 13 bold") .grid(row=2,column=0,sticky=W)


Label(window,text="",bg="white",fg="black",font="none 12 bold") .grid(row=3,column=2,sticky=W)


Label(window,text=" Name:",bg="white",fg="black",font="none 10 bold") .grid(row=4,column=2,sticky=W)

name=Entry(window,width=40,bg="white")
name.grid(row=4,column=3)

Label(window,text="",bg="white",fg="black",font="none 10 bold") .grid(row=5,column=2,sticky=W)

Label(window,text=" Email Address:",bg="white",fg="black",font="none 10 bold") .grid(row=6,column=2,sticky=W)

email_address=Entry(window,width=40,bg="white")
email_address.grid(row=6,column=3)

Label(window,text="").grid(row=7,column=2)
Label(window,text=" Telephone Number:",bg="white",fg="black",font="none 10 bold").grid(row=8,column=2,sticky=W)

tel_nr=Entry(window,fg="black",width=40,bg="white")
tel_nr.grid(row=8,column=3)

Label(window,text="").grid(row=9,column=0)
Label(window,text="Pickup and Drop-off info:",bg="white",fg="black",font=("none 13 bold")).grid(row=10,column=0)

Label(window,text="Pickup Address:",bg="white",fg="black",font="none 10 bold").grid(row=11,column=2)
pickup_address=Text(window,bg="white",fg="black",height=6,width=30)
pickup_address.grid(row=12,column=2)

Label(window,text="Dropoff Address:",fg="black",bg="white",font="none 10 bold").grid(row=11,column=3)
dropoff_address=Text(window,fg="black",bg="white",width=30,height=6)
dropoff_address.grid(row=12,column=3)

Label(window,text="").grid(row=13,column=0)

Label(window,text="Pickup Date:",bg="white",fg="black",font="none 10 bold").grid(row=14,column=2,sticky=W)
pickup_date=Entry(window,bg="white",fg="black",width=40)
pickup_date.grid(row=14,column=3)

Label(window,text="").grid(row=15,column=0)

Label(window,text="Pickup Time:",bg="white",fg="black",font="none 10 bold").grid(row=16,column=2,sticky=W)
pickup_time=Entry(window,bg="white",fg="black",width=40)
pickup_time.grid(row=16,column=3)

Label(window,text="").grid(row=17,column=0)

Label(window,text="Dropoff Date:",bg="white",fg="black",font="none 10 bold").grid(row=18,column=2,sticky=W)
dropoff_date=Entry(window,bg="white",fg="black",width=40)
dropoff_date.grid(row=18,column=3)

Label(window,text="").grid(row=19,column=0)
Label(window,text="MTY Depot:",bg="white",fg="black",font="none 10 bold").grid(row=20,column=2,sticky=W)
MTY=Entry(window,bg="white",fg="black",width=40)
MTY.grid(row=20,column=3)

Label(window,text="").grid(row=21,column=0)
Button(window,text="CALCULATE!",bg="white",fg="red",width=80,height=2,command=invoice_calculate).grid(row=22,column=3)

#####################program starts######################
window.mainloop()

I am reasonably sure that this may be a logic issue.

1 Answer 1

1

This error occurs because you are passing a float as the argument for txt. I assume it is expecting a string, so change txt = distance to txt = str(distance) and it should work.
Here's the modified code:

def page2(self,distance,traveltime,total):
        self.set_xy(41.029, 11)
        self.set_font('Arial', 'B', 13)
        self.set_text_color(0, 0, 0)
        self.cell(w=25.273, h=3.276, align='L', txt=str(distance), border=0)   
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help. If this resolved your problem, please accept this answer with the check mark button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.