簡體   English   中英

tkinter 輸入框 - 可選輸入

[英]tkinter entry boxes - optional input

我的 gui 中有四個輸入框,無論我在其中輸入什么信息, .get() 都可以正常工作。 問題在下面的條件下進一步開始。 這些輸入框的原因是過濾了一個 dataframe

第一個條件在 OR (|) 中有幾個條件。 這些條件被打包為“conditionORall”,其他條件同時具有一個條件。 目標是根據用戶輸入過濾 dataframe。 使用此代碼,這僅在我填寫所有四個輸入框時才有效,因為條件是 (&)。 例如,我希望能夠搜索 20220101 結束日期之前的所有數據。結果應該是給定日期的所有數據,而無需輸入更多框。 現在,如果我將一個輸入框留空,它會導致一張空白表。

有沒有辦法以某種方式將這些輸入框設置為 default=True 以便在留空時考慮所有信息? 例如,我還嘗試使用 (len(condition6) > 0 來動態創建最終條件。但它只是沒有用。這可能是一個非常簡單的問題,但在過去的 6 小時內我無法弄清楚。謝謝你為你提供幫助。


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'))

如果你想讓一個過濾器可選,你可以考慮默認返回值為True ,除非過濾器是非空的 這可以通過以下方式完成:

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)] 

您可以使用相同的方法在將來添加更多可選過濾器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM