0
我想添加基於在GUI中選擇raidoButton的不同字典。基於raidoButton的值更新不同的字典?
目前我可以附加一個選擇字典,但是我試圖創建一些東西,將檢查選定的raidButton(0,1或2) 的值,然後用該值檢查字典名稱的另一個字典需要追加。
注意:我的字典存儲在與腳本相同的目錄中的程序之外的文件中。
這是我的代碼。
thislib = IntVar() # thislib is the variable for the raidoButtons
def append_notes(event=None):
buttonValue = thislib.get()
list_of_dicts = {0:vzt_notes, 1:rms_notes, 2:nsr_notes}
dict_to_be_updated = list_of_dicts[buttonValue]
e1Current = keywordEntry.get().lower()
e1Upper = keywordEntry.get().upper()
e2Current = root.text.get(1.0, END)
answer = tkinter.messagebox.askquestion("Update Notes!","Are you sure you want update your Notes for "+e1Upper+" This cannot be undone!")
if answer == "yes":
dict_to_be_updated[e1Current] = e2Current
with open("%s" %dict_to_be_updated, "r+") as working_temp_var:
json.dump(dict_to_be_updated, working_temp_var, indent = "")
else:
tkinter.messagebox.showinfo('...','That was close!')
當我得到下面的錯誤,我認爲它說我with open
發言時它試圖把字典作爲文件名/路徑的內容。我不知道如何解決這個問題。我只想通過使用raidoButton值作爲索引值從list_of_dicts獲取文件名。
with open("%s" %dict_to_be_updated, "r+") as working_temp_var:
FileNotFoundError: [Errno 2] No such file or directory: '{\'dss\': \'# DSS: Decision Support Systems\\n# U
也許我不能這樣做,但它會很高興知道我是否可以做一些更舒服的事情。否則,我必須爲每個添加爲筆記存儲位置的字典創建一個長的if: elif: elif: else:
類型的語句。
目前尚不清楚爲什麼要將字典傳遞給'open'函數;這是打開文件。 –
'丹尼爾羅斯曼'我試圖通過'打開'功能的字典的名稱。我可以從錯誤代碼中看到它傳遞的是內容,如果是字典而不是名稱。我想弄清楚如何傳遞這個名字。 |我也只是添加了錯誤。 –
對象不知道他們的名字。如果您需要傳遞名稱,請保留一個單獨的名稱列表並在其中查找。 –