Home > Mobile >  How do you update an element in a child window with a value from anelement in a main window in PySim
How do you update an element in a child window with a value from anelement in a main window in PySim

Time:02-24

I feel that I have previously solved this problem but now I cannot fathom it. I have a main window with two tabs. On one of the tabs I have an input element for a name [-PATIENT-]. On exiting the element, if the name that was entered is not in a list from the database table a message alerts me to the fact and asks whether I want to add the new name to the database table. If the reply is 'OK' then a child window (add_patient_window) opens for entering the data of the new person for saving. To avoid mistakes I want the name that goes into the ['-NAME-'] element in the child window to have the exact value of the ['-PATIENT-] window in the Tab. For doing so there is a button in the child window 'XFER'. All goes well until it gets to the update of the ['-NAME-'] element. Below is the relevant code. You will notice that I have created a function to obtain the value of '-PATIENT- from the TAB and formatted it to capitalise the first letters. It prints the name as expected. When the 'XFER' button is clicked on the child window (add_patient_window) I want it to run get_new_name() and then update the ['-NAME-] element in the child window with the value of variable newname derived from ['-PATIENT-']. I have tried the update in the function as well as the event but neither works. Help will be appreciated.

def get_new_name():
    newname=values['-PATIENT-'] #This is an element in the main window tab
    newname=newname.title()
    print('the new patient is :'   newname)
    # window['-NAME-'].update(newname) #This element is in the child window

def add_patient_window():
    """
    Function to create secondary window
    """
    add_patient_layout=[
        
        [sg.Push(),sg.T('Add a patient',text_color='blue',font=40),sg.Push()],
        [sg.T('')],
        [sg.T('Patient name'),sg.In(size=30, key='-NAME-')],
        #[sg.T('Date purchased:'),sg.CalendarButton('Calendar',  target='-IN5-', key='_DATE_',format='%Y-%m-%d'),sg.In(key='-IN5-',size=10)],
        [sg.T('Patient ID:'),sg.Push(),sg.In(size=10,key='-PATID-')],
        [sg.T('Patient date of birth:'),sg.Push(),sg.In(size=10,key='-DOB-')],
        [sg.T('Postal address:'),sg.Push(),sg.In(size=10,key='-PADRESS-')],
        [sg.T('Street address:'),sg.Push(),sg.In(size=10,key='-ST_ADRESS-')],
        [sg.T('Town:'),sg.Push(),sg.In(size=10,key='-TOWN-')],
        [sg.T('Post Code:'),sg.Push(),sg.In(size=10,key='-PCODE-')],
        [sg.T('Cellphone No:'),sg.Push(),sg.In(size=10,key='-PCELL-')],
        [sg.T('Email address:'),sg.Push(),sg.In(size=10,key='-PEMAIL-')],
        [sg.T('')],
        [sg.Button('Save',size=8,button_color='red',key='-SAVE-'),sg.Push(),sg.Button('Xfer name',size=8,key='-XFER-',button_color='green'),sg.Push(),sg.Button('Quit',size=8,button_color='blue')]
        
    ]
    add_patient_window=sg.Window('Add a new patient', add_patient_layout, modal=True)#Secondary window

    while True:                             # Event loop for secondary window

        event,values=add_patient_window.read()  # Read secondary window
        if event in (None,'Quit'):
            break

        elif event == '-SAVE-':
            conn=sqlite3.connect(r'/home/bushbug/Databases/FSCashBook')
            cur3=conn.cursor()
            sql3=("Select Id from Patients Desc")
            result=cur3.execute(sql3)
            for row in result:
                LastId=row[0]
                Id=LastId
                Id=Id 1
                print(Id)

            Id=Id
            Patient_name=values['-NAME-']
            Patient_ID=values['-PATID-']
            Patient_DOB=values['-DOB-']
            PostAdd=values['-PADRESS-']
            StAdd=values['-ST_ADRESS-']
            Town=values['-TOWN-']
            PostCode=values['-PCODE-']
            Cell_phone=values['-PCELL-']
            E_mail=values['-PEMAIL-']
            print(Id,Patient_name,Patient_ID,Patient_DOB,PostAdd,StAdd,Town,PostCode,Cell_phone,E_mail)
            cur4=conn.cursor()
            my_patient_data=(Id,Patient_name,Patient_ID,Patient_DOB,PostAdd,StAdd,Town,PostCode,Cell_phone,E_mail)
            print(my_patient_data)
            Sql4="INSERT INTO Patients (Id,pName,pID,pDOB,pPostAdd,pStAdd,pTown,pPostCode,pCell,pmail) VALUES (?,?,?,?,?,?,?,?,?,?)"
            cur4.execute(Sql4,my_patient_data)
            conn.commit()
            sg.popup('Save','You have successfully saved the record!')

        elif event=='-XFER-':

            get_new_name()
            window['-NAME-'].update(newname) #Element in the child window
            
    add_patient_window.close()

CodePudding user response:

Most of time, I passed values or variables by arguments of function, global variables or class attributions.

Following code show a simple case

import PySimpleGUI as sg

def popup(username):

    username = username.title()
    if username == '':
        username = "my friend"
    sg.theme("DarkBlue4")
    layout = [
        [sg.Text(f"Nice to meet you, {username} !")],
        [sg.Push(), sg.Button('OK')],
    ]
    sg.Window('Welcome', layout, modal=True).read(close=True)

sg.theme('DarkBlue3')
layout = [
    [sg.Text("What's your name")],
    [sg.Input(key='-NAME-')],
    [sg.Button('SEND')],
]
window = sg.Window("test", layout)

while True:

    event, values = window.read()

    if event in (sg.WINDOW_CLOSED, 'Exit'):
        break
    elif event == 'SEND':
        username = values['-NAME-']
        popup(username)

window.close()
  • Related