KevOMalley743
KevOMalley743

Reputation: 581

Taking input from pysimplegui and passing it to a function (python)

I'd like to allow the user to copy and paste a folder address using into a pysimplegui UI and then use that text to get a list of the files in the folder.

import PySimpleGUI as psg
import pathlib as pl

#set the theme for the screen/window
psg.theme("LightPurple")

#define layout
layout=[[psg.Text("Folder Address",size=(15, 1), font='Lucida',justification='right'),psg.Input()],
    [psg.Button("SAVE", font=("Times New Roman",12)),psg.Button("CANCEL", font=("Times New Roman",12))]]

#Define Window
win =psg.Window("Data Entry",layout)
#Read  values entered by user and pass that input to a windows path
v=win.read()
d = pl.WindowsPath(v)# I'm pretty sure this is where I'm going wrong. 

#close first window
win.close()

#define layout for second windows to display data entered by user in first window
layout1=[[psg.Text("The data you entered is  :", size=(20,1), font='Lucida', text_color='Magenta')],
        [psg.Text(str([e for e in dir_path.iterdir()]), font='Lucida', text_color='Blue')]]

#Define Window and display the layout to print output        
win1=psg.Window("Output Screen",layout1)

e,v=win1.read()
#close second window
win1.close()

I'm happy to play around with the formatting etc, but if anyone had any insight (or a good tutorial on pysimplegui) it would be much appreciated.

Upvotes: 1

Views: 2275

Answers (1)

Jason Yang
Jason Yang

Reputation: 13051

After create layout of window, then wait event from keyboard or mouse inputs in event loop. Again and again, until window closed or script end.

You can do everthing in one window, revised code as following. For long output, use sg.Multiline here.

import PySimpleGUI as psg
import pathlib as pl

#set the theme for the screen/window
psg.theme("LightPurple")

font1 = ('Lucida', 12)
font2 = ("Times New Roman",12)
#define layout
layout=[
    [psg.Text("Folder Address",size=(15, 1), font=font1, justification='right'),
     psg.Input(key='-INPUT-')],
    [psg.Button("SAVE", font=font2),
     psg.Button("CANCEL", font=font2)],
    [psg.Text("The data you entered is  :", size=(20,1), font=font1, text_color='Magenta')],
    [psg.Multiline('', size=(80, 25), font=font1, text_color='Blue', key='-OUTPUT-')],
]

#Define Window
win = psg.Window("Data Entry",layout)

# Event loop
while True:

    event, values = win.read()
    if event in (psg.WINDOW_CLOSED, 'CANCEL'):
        break
    elif event == 'SAVE':
        v = values['-INPUT-']
        d = pl.WindowsPath(v)
        text = str([e for e in d.iterdir()])
        win['-OUTPUT-'].update(value=text)

#close window
win.close()

Upvotes: 2

Related Questions