如何更新读取excel文件的PySimpleGUI列表框

2024-04-28 19:36:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python3.7,这是当前的代码库(很抱歉,我放了这么多代码,但我认为这会有帮助)

def TRADE_ENTRY(df_names, df_underlyings,df_strategies, columns, param, out_path,recovery_path):
   nameUpdate =0
   strategyUpdate=0
   underlyingUpdate=0
   sg.theme('Dark Brown 1')
   listing = [sg.Text(u, size = param) for u in columns]
   header =   [[x] for x in listing]
   now = datetime.datetime.now()
   core = [
   sg.Input(f"{now.month}/{now.day}/{now.year}",size = param),
   sg.Input(f"{now.hour}:{now.minute}:{now.second}",size = param),
   sg.Listbox(list(df_strategies.STRATEGIES), size=(20,2), enable_events=False, key='_PLAYERS0_'),
   sg.Listbox(['ETF', 'EQT', 'FUT', 'OPT', 'BOND'],enable_events=False,key='_PLAYERS20_',size = (20,2)),
   sg.Listbox(list(df_names.NAMES), size=(20,4), enable_events=False,key='_PLAYERS6_'),
   sg.Listbox( ['B', 'S'],size = (20,1),enable_events=False,key='_PLAYERS12_'),
   sg.Input(size = param),
   sg.Input(size = param),
   sg.CalendarButton('Calendar', pad=None, font=('MS Sans Serif', 10, 'bold'), 
                button_color=('yellow', 'brown'), format=('%d/%m/%Y'),  key='_CALENDAR_', target='_INP_'),
   sg.Input(size = param),
   sg.Listbox(list(df_underlyings.UNDERLYINGS), size=(20,4), enable_events=False,key='_PLAYERS2_'),
   sg.Listbox(['C', 'P', 'N/A'],size = param), 
   ]

   mesh = [[x,y] for (x,y) in list(zip(listing, core))]
   mesh[8].append(sg.Input(size = (10,2),key = '_INP_'))
   layout =[[sg.Button("SEND"),sg.Button("NEW_NAME"), sg.Button("NEW_STRAT"), sg.Button("NEW_UND")] ]+ mesh
   window = sg.Window('Trade Entry System', layout, font='Courier 12').Finalize()
   
   while True:
      event, values = window.read(timeout=500)
      #print('EVENT, VALUES', event, values)# all the inputs with extra information for compiler
      if event == "SEND":
         data = values
         a = list(data.values())

         a = [x if isinstance(x, list) == False else empty_handler(x) for x in a]
         a = [x if x !="" else "EMPTY" for x in a ]
         #print('A', a)#all the inputs now in a list
         df = pd.DataFrame(a, index = columns)
         print('DF1', df)#columns dataframe with column names and then the values
         df = df.transpose()
         #print('DF2', df)#rows dataframe with column names and then the values
         status = error_handling(df)
         #print('STATUS', status)
         if status == "ERROR":
            print("YOU MUST RECTIFY INPUT")

         elif status == "CORRECT":
            #if a future then will overwrite its name
            if df['TYPE'][0] == "FUT":
               df['NAME'][0] = "F-"+  df['UNDERLYING'][0] + "-" +df['EXPIRATION'][0] 
            #if an option then will overwrite its name
            elif df['TYPE'][0] =="OPT":
               df['NAME'][0] = 'O-' +  df['UNDERLYING'][0] + "--" + df['OPTION_TYPE'][0] +df['STRIKE'][0] +"--" +df['EXPIRATION'][0]
            else:
               pass 
            processing(df, recovery_path, out_path)
         else:
            print("ERROR WITH USER INPUT FATAL")
            break
       elif event == "NEW_NAME":
         security_creation(r'Y:\NAMES.xlsx', "Sheet1", "NAME", param)
         nameUpdate=1
         continue
      elif event == "NEW_STRAT":
         security_creation(r'Y:\STRATEGIES.xlsx', "Sheet1", "STRATEGY", param)
         strategyUpdate=1
         continue
      elif event == "NEW_UND":
         security_creation(r'Y:\UNDERLYINGS.xlsx', "Sheet1", "UNDERLYINGS", param)
         underlyingUpdate=1
         continue
         
      elif event == sg.TIMEOUT_KEY:
         if(nameUpdate==1):
            df_names = pd.read_excel(r'Y:\NAMES.xlsx', "Sheet1")
            df =df_names.values.tolist()
            window['_PLAYERS6_'].update(values=df, set_to_index=0)
         
         if(underlyingUpdate==1):
            df_underlyings = pd.read_excel(r'Y:\UNDERLYINGS.xlsx', "Sheet1")
            df =df_underlyings.values.tolist()
            window['_PLAYERS2_'].update(values=df, set_to_index=0)
         
         if(strategyUpdate==1):
            df_strategies = pd.read_excel(r'Y:\STRATEGIES.xlsx', "Sheet1")      
            df =df_strategies.values.tolist() 
            window['_PLAYERS0_'].update(values=df, set_to_index=0)
         
         print("Listboxes updated !")
      else:
         print("OVER")
         break
   window.close()

TRADE_ENTRY(df_names, df_underlyings,df_strategies, columns, param,out_path, recovery_path)

函数末尾有3个elif,所有新名称、新策略和新UND都是用户向相应的3个excel文件提交信息。函数security_creation实际上更新了上述excel文件。下面我正在尝试更新列表框,但运气不好

我很困惑,如果有任何帮助,我将不胜感激


Tags: pathkeyeventdfforsizeifparam