Pysimplegui : input data from table to docxtpl

140 Views Asked by At

I am building a little script to make faster word documents. With the help of docxtpl and pysimplegui

Now I want to create a table to fill in data.

My table layout looks like this:

headings = ['test1', 'test2', 'test3','test4','Result']
header =  [[sg.Text('  ')] + [sg.Text(h, size=(12)) for h in headings]]

input_rows = [[sg.Input(size=(10,1), pad=(20,0)) for col in range(5)] for row in range(4)]

But can't find the right way to use sg.Input with a key reference like in a normal input data field :

[sg.Text("Year:", (10, None)), sg.Input("",size=(10,1), key="YEAR", do_not_clear=False)],

How can I use this in my table?

Thanks in advance!

Kind regards

2

There are 2 best solutions below

0
Edoardo Balducci On

maybe using a key pattern? Something like this:

input_rows = [[sg.Input(size=(10,1), pad=(20,0), key=f'input_{row}_{col}') for col in range(5)] for row in range(4)]

So that it combines the row and column numbers to make each key unique.

0
Jason Yang On

You can access the element in the table by using tuple key, like window[(row, col)].

Example Code

import PySimpleGUI as sg


rows, cols, width = 10, 5, 10
headings = [f"Column {i}" for i in range(5)]
data = [[(row+1)*(col+1) for col in range(cols)] for row in range(rows)]

settings = {
    "font"          : ("Courier New", 12),
    "justification" : "center",
    "size"          : width,
    "pad"           : (0, 0),
}

layout_table = [
    [sg.Text(heading, **settings) for heading in headings]] + [
    [sg.Input(data[row][col], key=(row, col), **settings)
        for col in range(cols)]
            for row in range(rows)
]

layout = [
    [sg.Column(layout_table, key='TABLE')],
    [sg.Push(), sg.Button("Send"), sg.Button("Next", bind_return_key=True, visible=False), sg.Push()],
]

window = sg.Window("Demo", layout, finalize=True)
window[(0, 0)].update(select=True)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    elif event == "Next":
        element = window.find_element_with_focus()
        while True:
            element = element.get_next_focus()
            if isinstance(element, sg.Input):
                break
        element.set_focus()
        element.update(select=True)

window.close()

enter image description here