-1

I have a script that runs fine on dell, but seems to work only 70% on mac. It basically let's the user select different parameters (dates, files, from lists etc), on a while loop until the user presses one of the create buttons that closes the loop.

I removed some parameters for security, but this is the base of the code. The while loop doesn't seem to work, the 3 create buttons don't work, the datepicker doesn't work. These is my first question, please be kind :)

sg.theme("LightBlue3")
func = ['get_positions_by_account', 'get_positions_by_platform', 'get_positions_by_platform_and_account']
select_all = sg.Listbox(POSITIONS_COLS, select_mode='multiple', key="-IN7-", size=(30,12))

layout = [
        [sg.Text("Select account and press go or press PASS to move to select platform:")],
        [sg.Combo(ALL_ACCOUNT, key="-IN1-",change_submits=True), sg.Button("GO"), sg.Button("PASS")],
        [sg.Text("Select platform:"), sg.Combo([], key="-IN2-",change_submits=True)],
        [sg.Text("select run date or date of local file:"), sg.Input(size=(20, 1), key="-IN5-", change_submits=True),
        sg.CalendarButton('select', target="-IN5-", key="-IN5-", format='%Y%m%d', default_date_m_d_y=(11, None, 2021))],
        [sg.Text("select minimal purchase date:"), sg.Input(size=(20, 1), key="-IN6-", change_submits=True),
        sg.CalendarButton('select', target="-IN6-", key="-IN6-", format='%Y-%m-%d', default_date_m_d_y=(11, None, 2021))],
        [sg.Text("If local file select file: "), sg.InputText(key="-IN3-", change_submits=True), sg.FileBrowse(key="-IN13-")],
        [sg.Text("Select function to run:"), sg.Combo(func, key="-IN4-",change_submits=True)],
        [sg.Text('Select position columns:'), sg.Button("Select All"), sg.Button("Top 23"), sg.Button("Clear All")],
        [select_all],
        [sg.Button("Create position file"), sg.Button("Create transfer file"), sg.Button("Create Talya file"),sg.Button("Exit")]]

window = sg.Window('PIZZA SLICER - Maya Moshe 2021', layout, size=(600, 500))


if __name__ == '__main__':
    account, platform, func1, date, pos_file, position, purchase_d = "", "", "", "", "", "", ""
    while True:
        event, values = window.read()
        account = values["-IN1-"]
        platform = values["-IN2-"]
        pos_file = values["-IN3-"]
        func1 = values["-IN4-"]
        date = values["-IN5-"]
        purchase_d = values["-IN6-"]
        pos_cols = values["-IN7-"]
        if event == sg.WIN_CLOSED or event == "Exit":
            print('Exiting program')
            exit()
        elif event == "GO":
            window["-IN2-"].update(value='', values=ACCOUNTS_PLATFORMS[account])
            platform = values["-IN2-"]
        elif event == "PASS":
            window["-IN2-"].update(value='', values=ALL_PLATFORMS)
            platform = values["-IN2-"]
        elif event == 'Select All':
            cols = POSITIONS_COLS
            window.Element('-IN7-').SetValue(cols)
        elif event == 'Top 23':
            cols = top20
            window.Element('-IN7-').SetValue(cols)
        elif event == "Clear All":
            window.Element('-IN7-').set_value([])
        elif event == 'Create position file':
            window.close()
            now = datetime.datetime.now().replace(microsecond=0)
            if pos_file:
                position = pd.read_csv(pos_file, dayfirst=True)
            eval(func1 + "(date=date, account=account, platform=platform, position=position, purchase_d=purchase_d, cols=pos_cols)")
            diff = datetime.datetime.now().replace(microsecond=0) - now
            print(f'pos filter, run time {diff}')
            exit()
        elif event == "Create transfer file":
            window.close()
            now = datetime.datetime.now().replace(microsecond=0)
            if pos_file:
                position = pd.read_csv(pos_file, dayfirst=True)
            get_positions_by_account(date=date, account=account, platform=platform, position=position, purchase_d=purchase_d, cols=pos_cols, transfer_flag=True)
            diff = datetime.datetime.now().replace(microsecond=0) - now
            print(f'Create transfer file, run time {diff}')
            exit()
            break
        elif event == "Create Talya file":
            window.close()
            now = datetime.datetime.now().replace(microsecond=0)
            if pos_file:
                position = pd.read_csv(pos_file, dayfirst=True)
            get_positions_by_account(date=date, account=account, platform=platform, position=position, purchase_d=purchase_d, cols=pos_cols, talya_flag=True)
            diff = datetime.datetime.now().replace(microsecond=0) - now
            print(f'Create Talya file, run time {diff}')
            exit()
Finops
  • 1
  • "don't work" mean nothing, what the failure symptoms or message ? – Jason Yang Dec 02 '21 at 07:45
  • @JasonYang when, for example, the user clicks the 'Exit' button - nothing happens. But the required response is printing a message and exit(). When clicking the 'Select All', the set_value doesn't work. – Finops Dec 02 '21 at 08:28

1 Answers1

0

Example Code here to show both of buttons Exit and Select All work. Nothing wrong found, maybe wrong code missed in your demo code.

import PySimpleGUI as sg

lst1 = list("ABCDEFGHIJ")

layout = [
    [sg.Button('Select All')],
    [sg.Listbox(lst1, size=(5, 10), select_mode='multiple', key='-IN7-')],
]
window = sg.Window("test", layout, finalize=True)
while True:

    event, values = window.read()

    if event in (sg.WINDOW_CLOSED, 'Exit'):
        print('Exiting program')
        break
    # Get value of elements should be here to prevent value of `values` is None when sg.WINDOW_CLOSED
    pos_cols = values["-IN7-"]
    if event == 'Select All':
        window['-IN7-'].set_value(lst1)

window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23