Pranav Khilnani
Pranav Khilnani

Reputation: 1

How to get automatic entry in my field in pysimplegui

layout = [
            [sg.Text("Enter Full Name :"), sg.InputText(key="name")],
            [sg.Text("Gender"), sg.Combo(["Male", "Female"], key="gender")],
            [sg.Text("Enter Enrollment No. :"), sg.InputText(key="enrollno")],
            [sg.Text("Exam Roll No. :"), sg.InputText(key="rollno")],
            [sg.Text("Medium :"), sg.InputText(key="med")],
            # [sg.Text("Compulsory English :"), sg.InputText(key="CE")],
            # [sg.Combo(["Supplementary English","Hindi", "Marathi"]), sg.InputText(key="SL")],
            [sg.Text("Theory 1 :"), sg.InputText(key="T1"), sg.Text("Practical 1 :"), sg.InputText(key="P1"),
             sg.Text("Internal Assessment 1 :"), sg.InputText(key="IA1"),
             sg.Text("Paper 1 Total :")],
            [sg.Text("Theory 2 :"), sg.InputText(key="T2"), sg.Text("Practical 2 :"), sg.InputText(key="P2"),
             sg.Text("Internal Assessment 2 :"), sg.InputText(key="IA2"),
             sg.Text("Paper 2 Total :")],
            [sg.Text("Paper Total")],
            [sg.Button("Submit", key="submit")],
        ]

what I want to do is, I am taking inputs of different subjects, I want that as the user enters the marks the total field gets updated automatically.

Upvotes: 0

Views: 30

Answers (1)

Jason Yang
Jason Yang

Reputation: 13051

Need to process all the events generated for all the Input elements, then sum the values to update other elements.

Example Code

import PySimpleGUI as sg

sg.theme('DarkBlue4')
sg.set_options(font=("Courier New", 12))

layout = [
    [sg.Input('0.0' if col==3 else '', size=8, justification='r', disabled=(col==3), enable_events=(col!=3), key=(row, col)) for col in range(4)] for row in range(3)] + [
    [sg.Push(), sg.Input('0.0', size=8, justification='r', disabled=True, key='Total')],
    [sg.Push(), sg.Button("Submit", key="submit")],
]
window = sg.Window("Title", layout, finalize=True)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    print(event)
    if isinstance(event, tuple):
        row, col = event
        subtotal = 0.0
        for col in range(3):
            try:
                if values[(row, col)]:
                    subtotal += float(values[(row, col)])
            except ValueError:
                subtotal = 'Error'
                break
        window[(row, 3)].update(subtotal)
        window.refresh()
        total = 0.0
        for row in range(3):
            try:
                text = window[(row, 3)].get()
                print(text)
                if text:
                    total += float(text)
            except ValueError:
                total = 'Error'
                break
        window['Total'].update(total)

window.close()

enter image description here

Upvotes: 0

Related Questions