0

First, I have read the docs. I understand that the information is stored in Key= x. My issue is when I call a function from another file it does not recognize x. I have read the docs, but failing to understand how to use the key

I tried putting x into a variable and passing it to the function.

File 1

def add_details():
    today1 = date.today()
    today2 = today1.strftime("%Y/%m/%d")
    create = str(today2)
    name = str(_name_)
    reason = str(_reason_)
    startDate = str(_startDate_)
    endDate = str(_endDate_)
    add_data(create,name,reason,startDate, endDate)

def add_data(create,name,reason,startDate, endDate):
    engine.execute('INSERT INTO schedule(Created_On, Fullname, reason, Start_Date, End_Date ) VALUES (?,?,?,?,?)',(create,name,reason,startDate,endDate))

File 2

while True:      
    event, values = window.Read() 
    print(event, values)       
    if event in (None, 'Exit'):      
        break  
    if event == '_subdate_': #subdate is the button Submit 
        sf.add_details()

My expected results are that the inputs of the GUI are passed to the function, then off to a SQLite db.

Error: name 'name' not defined (or any key variable)

RobE
  • 93
  • 2
  • 11
  • It would be helpful to see your layout so that the keys you're trying to use are shown. You're missing a critical step in retrieving the actual values. The values of your fields are returned through the variable values. They keys you are trying to use (_name_) should be written was values['_name_']. This will give you the value of the input element you've defined as having the key '_name_'. Take a look at the basic PySimpleGUI Design Patterns that collect input values and show how to access them in the docs http://www.PySimpleGUI.org – Mike from PSG Oct 20 '19 at 14:59

1 Answers1

0

This is an example that you'll find running on Trinket (https://pysimplegui.trinket.io/demo-programs#/demo-programs/design-pattern-2-persistent-window-with-updates)

It shows how keys are defined in elements and used after a read call.

import PySimpleGUI as sg      

"""
  DESIGN PATTERN 2 - Multi-read window. Reads and updates fields in a window
"""

# 1- the layout
layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(15,1), key='-OUTPUT-')],
          [sg.Input(key='-IN-')],
          [sg.Button('Show'), sg.Button('Exit')]]

# 2 - the window
window = sg.Window('Pattern 2', layout)

# 3 - the event loop
while True:
    event, values = window.read()
    print(event, values)
    if event in (None, 'Exit'):
        break
    if event == 'Show':
        # Update the "output" text element to be the value of "input" element
        window['-OUTPUT-'].update(values['-IN-'])

        # In older code you'll find it written using FindElement or Element
        # window.FindElement('-OUTPUT-').Update(values['-IN-'])
        # A shortened version of this update can be written without the ".Update"
        # window['-OUTPUT-'](values['-IN-'])     

# 4 - the close
window.close()
Mike from PSG
  • 5,312
  • 21
  • 39