Python Forum

Full Version: Tkinter move to next row
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, I'm making an app that pastes receipt details into an excel spreadsheet for me & I have 2 problems that are driving me crazy!

1. For sec1: Tkinter prompts, it gives me a prompt to upload a receipt to be scanned & it works however it does not submit the receipt to the excel file until the 2nd prompt "would you like to submit another receipt" appears and I click no. This is true even if I submit 5 receipts it will only submit the first.

2. Sec3: Export to Excel. This should be simple but I can't get the data from the receipt to move down to the next column every time I submit a new receipt. It is overwriting the last receipt in the same column. I'm guessing I need either a for loop or a counter (I'm new to programming). Appreciated all!

################## Sec1: Tkinter prompts ####################################
# display message in a child window
from tkinter import *
from tkinter import filedialog

def messageWindow():
    root.filename =  filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
    # create child window
    win = Toplevel()
    # display message
    message = "Would you like another?"
    
    # quit child window and return to root window
    # the button is optional here, simply use the corner x of the child window
    Label(win, text=message).pack()
    Button(win, text='Yes', command=yes, ).pack()
    Button(win, text='No', command=no, ).pack()
    
    x= root.filename
   
    global h
      ## this makes the file address grabbable
    h=x
    
def yes():
    
    messageWindow()
   
    
    
def no():
    root.destroy()
    
    
# create root window
root = Tk()
# put a button on it, or a menu
Button(root, text='Upload Image', command=messageWindow).pack()

# start event-loop

root.mainloop()
################## Sec2: Receipt Recognition Section ##################


import requests


url = 'https://api.taggun.io/api/receipt/v1/simple/file'

headers = {'apikey': 'apikeyleftout'}

files = {
  'file': (
    r'{}'.format(h), # set a filename for the file
    open(r'{}'.format(h), 'rb'), # the actual file, r' is to do with text format
    'image/jpg'), # content-type for the file

  # other optional parameters for Taggun API (eg: incognito, refresh, ipAddress, language)
  'incognito': (
    None, #set filename to none for optional parameters
    'false') #value for the parameters
  }

response = requests.post(url, files=files, headers=headers)

print(response.content)


resp= response.json()  ## allows me to grab from json figures

Merchant= resp["merchantName"]   ## grabs total amount & data
Merchant_name= (Merchant["data"])  ## grabs data figure

total= resp["totalAmount"]   ## grabs total amount & data
totalvaluedata= (total["data"])  ## grabs data figure

tax= resp["taxAmount"]   ## grabs total amount & data
tax_value= (tax["data"])  ## grabs data figure
################ Sec3: Export to Excel ###############################################################
#

#
import xlsxwriter

workbook = xlsxwriter.Workbook('Receipts.xlsx')

worksheet7 = workbook.add_worksheet()



currency_format = workbook.add_format({'num_format': '$#,##0'})

# Some sample data for the table.
data = [
    [Merchant_name, tax_value, totalvaluedata]
  
  

]


caption = 'Receipts Table'

# Set the columns widths.
worksheet7.set_column('C:G', 12)
worksheet7.set_column('B:B', 50)

# Write the caption.
worksheet7.write('B1', caption)

# Add a table to the worksheet.
worksheet7.add_table('B3:D200', {'data': data,
                               'columns': [{'header': 'Merchant'},
                                           {'header': 'Tax'},
                                           {'header': 'Total'},
                                          
                                           ]})


workbook.close()
################################################################################

Output:
b'{"totalAmount":{"data":35.85,"confidenceLevel":0.83},"taxAmount":{"data":3.05,"confidenceLevel":0.9},"confidenceLevel":0.622,"date":{"confidenceLevel":0},"merchantName":{"data":"Bulk Barn","confidenceLevel":0.81},"merchantAddress":{"data":"393 King Street West, Toronto ON, Canada","confidenceLevel":0.81},"merchantTypes":{"data":["Food & Drink Shop"],"confidenceLevel":0.81}}'

No tracebacks
The logic is wrong. First forget about the excel part: you can use standard output to see the result of the scan. Try to change your code so that it prints the response.content before it displays the tkinter window asking if you want to upload another file.
Will try that Gribouillis, cheers