What am I missing to get an output in this data entry form?

54 views Asked by At

I have 2 docx files and I want to have 2 separate data entry forms based on those. This is the code I wrote until now, but there is no output whenever I click Create Contract. Initially I had 2 separate py files for each docx and in that way I was able to output, but I want to make it simple and clearer.

import datetime
from pathlib import Path
import PySimpleGUI as sg
from docxtpl import DocxTemplate

# Initialize PySimpleGUI
sg.theme("Default1")

# Define the document paths and create DocxTemplate objects
vendor_document_path = Path.cwd() / "vendor-contract.docx"
gdpr_document_path = Path.cwd() / "gdpr.docx"
vendor_doc = DocxTemplate(vendor_document_path)
gdpr_doc = DocxTemplate(gdpr_document_path)

# Get the current date and date one week from today
today = datetime.datetime.today()
today_in_one_week = today + datetime.timedelta(days=7)

# Create the main layout with tabs for Vendor and GDPR contracts
main_layout = [
    [sg.TabGroup([
        [sg.Tab('Vendor Contract', [
            [sg.Text("Client name:"), sg.Input(key="CLIENT")],
            [sg.Text("Vendor Name:"), sg.Input(key="VENDOR")],
            [sg.Text("Amount:"), sg.Input(key="AMOUNT")],
            [sg.Text("Description1:"), sg.Input(key="LINE1")],
            [sg.Text("Description2:"), sg.Input(key="LINE2")]
        ])],
        [sg.Tab('GDPR Contract', [
            [sg.Text("Nume client:"), sg.Input(key="Nume")],
            [sg.Text("Cod Numeric Personal:"), sg.Input(key="Cod")]
        ])]
    ])],
    [sg.Button("Create Contract"), sg.Button("Exit")]
]

# Create the PySimpleGUI window
window = sg.Window("Contract Generator", main_layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED or event == "Exit":
        break
    elif event == "Create Contract":
        # Add calculated fields to the dict
        values["TODAY"] = today.strftime("%Y-%m-%d")
        values["TODAY_IN_ONE_WEEK"] = today_in_one_week.strftime("%Y-%m-%d")

        output_path = None  # Initialize output_path to None

        if values.get("Vendor Contract"):
            # Render the template for the vendor contract
            values["NONREFUNDABLE"] = round(float(values["AMOUNT"]) * 0.2, 2)
            vendor_doc.render(values)
            output_path = Path.cwd() / f"{values['VENDOR']}-contract.docx"
            try:
                vendor_doc.save(output_path)
            except Exception as e:
                sg.popup_error(f"Error saving the file: {str(e)}")
        elif values.get("GDPR Contract"):
            # Render the template for the GDPR contract
            gdpr_doc.render(values)
            output_path = Path.cwd() / f"{values['Nume']}-gdpr.docx"
            try:
                gdpr_doc.save(output_path)
            except Exception as e:
                sg.popup_error(f"Error saving the file: {str(e)}")

            sg.popup("File saved", f"File has been saved here: {output_path}")

window.close()
1

There are 1 answers

0
Jason Yang On

Should try to call print to debug what happened.

print(event, values)
print(repr(values.get("Vendor Contract")))
print(repr(values.get("GDPR Contract")))
Create Contract {'CLIENT': 'client name', 'VENDOR': 'vendor name', 'AMOUNT': 'amount', 'LINE1': 'desc1', 'LINE2': 'desc2', 'Nume': 'name client', 'Cod': 'Cod number', 0: 'GDPR Contract'}
None
None

You can find that there's no keys "Vendor Contract" and "GDPR Contract" in values, so it will be always False for your if-elif statements, then pass and do nothing.

Maybe you want to check which tab, then you can do it by

    if event == "Create Contract":
        tab = values[0]
        if tab == "Vendor Contract":
            pass
        elif tab == "GDPR Contract":
            pass