简体   繁体   中英

tkinter entry boxes - optional input

I have four entry boxes in my gui and whatever information I put in there, .get() works as it should. The problem starts further below on the conditions. The reason for these entry boxes is the filtering of a dataframe

The first condition has several conditions within with OR (|). These conditions are packed as "conditionORall", the other conditions have one condition at the time. The goal is to filter a dataframe based on user input. With this code, this works only if I fill out all four entry boxes as the conditions are with (&). I want to be able to for eg search for all data prior the end date of 20220101. An the result should be all data upon the given date, without entering more boxes. Right now it results in a blank sheet if I leave one entry box blank.

Is there a way to set these entry boxes somehow as default=True so it considers all information if left blank? for eg I also tried using (len(condition6) > 0 in order to create a final condition dynamically. But it just did not work. It is probably a very easy question but I couldn't figure it out in the past 6hrs. Thank you for you help.


namesec = namesec_execII.get().strip()
symbol = namesec_execII.get().strip()
start = start_execII.get().strip()
end = end_execII.get().strip()
segment = segment_execII.get().strip()



inputfile = 'testfile.pkl'
df = pd.read_pickle(inputfile)
 


conditionORall = ((df['namesec'] == namesec) | (df['localMnemonic'] == symbol) | (df['securityShortName'].str.contains(symbol)))
condition6 = (df['SegmentID'] == segment)
condition7 = (df['TradeDate'] >= start)
condition8 = (df['TradeDate'] <= end)



df = df.loc[(conditionORall) & (condition6) & (condition7) & (condition8)]        

df.to_excel('result.xlsx', index=False)




namesec_execII=StringVar()
namesec_entry_execII = Entry(my_frame10,textvariable = namesec_execII, bg='white', font=('Helvetica',12, 'normal'))

start_execII=StringVar()
start_entry = Entry(my_frame10,textvariable = start_execII, bg='white', font=('Helvetica',12, 'normal'))

end_execII=StringVar()
end_entry = Entry(my_frame10,textvariable = end_execII, bg='white', font=('Helvetica',12, 'normal'))

segment_execII=StringVar()
segment_entry = Entry(my_frame10,textvariable = segment_execII, bg='white', font=('Helvetica',12, 'normal'))

If you want to make a filter optional , you can consider the default return value to be True unless the filter is non-empty . This can be done in the following way:

conditionORall = ((df['namesec'] == namesec) | (df['localMnemonic'] == symbol) | (df['securityShortName'].str.contains(symbol)))
condition6 = (df['SegmentID'] == segment) if segment != "" else True
condition7 = (df['TradeDate'] >= start) if start != "" else True
condition8 = (df['TradeDate'] <= end) if end != "" else True

df = df.loc[(conditionORall) & (condition6) & (condition7) & (condition8)] 

You can use the same approach to add further optional filters in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM