2017-04-05 36 views
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:類型的語句。

+1

目前尚不清楚爲什麼要將字典傳遞給'open'函數;這是打開文件。 –

+0

'丹尼爾羅斯曼'我試圖通過'打開'功能的字典的名稱。我可以從錯誤代碼中看到它傳遞的是內容,如果是字典而不是名稱。我想弄清楚如何傳遞這個名字。 |我也只是添加了錯誤。 –

+3

對象不知道他們的名字。如果您需要傳遞名稱,請保留一個單獨的名稱列表並在其中查找。 –

回答

0

感謝Daniel Roseman的建議,我能夠使用單獨的列表來執行我正在尋找的功能。

下面是我的問題,如果有人感興趣的解決方案。

# This is the variable that stores the value of the selected radioButton 
thislib = intVar() 

def append_notes(event=None): 

    # This is getting the current value of the radooButton selected. Currently the posible values are 0,1, and 2. 
    buttonValue = thislib.get() 

    # This list is used during out "with open" statement below. 
    list_of_current_keys=["vzt_keys","rms_keys","nsr_keys"] 

    # This list is used during out "with open" statement below. 
    list_of_current_dicts=["vzt_notes","rms_notes","nsr_notes"] 

    # The two dictionaries and lists below are used in the below if statement in order to 
    # update the correct dictionary and list based of the returned radioButton value. 
    list_of_dicts = {0:vzt_notes, 1:rms_notes, 2:nsr_notes} 
    list_of_keys = {0:vzt_keys, 1:rms_keys, 2:nsr_keys} 
    dict_to_be_updated = list_of_dicts[buttonValue] 
    keys_to_be_updated = list_of_keys[buttonValue] 

    # This variable is getting the lower case version of the typed keyword in the keyword 
    # search box. This lets us search the key value of the dictionary without 
    # having to worry about case being an issue to see if the keyword already exist 
    e1Current = keywordEntry.get().lower() 

    # This is used to store the keyword in the format typed by the used. 
    # So the user can decide on the case they want displayed in the keyword list. 
    e1allcase = keywordEntry.get() 
    # e2Current is where all the notes are displayed or typed. we get the notes that are 
    # in the textbox to update or add to the selected dictionary as the keys : string value. 
    e2Current = root.text.get(1.0, END) 

    # This is a simple pop up window that will ask if you are sure you wish to update your notes. 
    # currently I have not written a way to undo the last update so for now this message is to 
    # warn you there is no going back if you hit the "yes" button. 
    answer = tkinter.messagebox.askquestion("Update Notes!","Are you sure you want update your Notes for "+e1allcase+" This cannot be undone!") 

    # This if statement will check take the currently selected dictionary & list and update them. 
    # If the keyword does not exist in the selected dictionary then It will add the keyword:"notes". 
    if answer == "yes": 
     current_library = list_of_dicts[buttonValue] 
     selected_dict = list_of_current_dicts[buttonValue] 
     selected_libkey = list_of_current_keys[buttonValue] 
     if e1Current in current_library: 
      statusW.config(text = "Updating Keyword & Notes for the "+selected_dict+" Library!") 
      dict_to_be_updated[e1Current] = e2Current 
      with open(selected_dict, "r+") as working_temp_var: 
       json.dump(dict_to_be_updated, working_temp_var, indent = "") 
      statusW.config(text = "Update Complete") 
     else: 
      statusW.config(text= "Creating New Keyword & Notes for the "+selected_dict+" Library!") 
      dict_to_be_updated[e1Current] = e2Current 
      with open(selected_dict, "r+") as working_temp_var: 
       json.dump(dict_to_be_updated, working_temp_var, indent = "") 
      keys_to_be_updated.append(e1allcase) 
      with open(selected_libkey, "r+") as working_temp_keys: 
       json.dump(keys_to_be_updated, working_temp_keys, indent = "") 
      statusW.config(text = "Update Complete") 
    # This calles a function that will update the display 
     update_kw_display() 
    else: 
     tkinter.messagebox.showinfo('...','That was close!')