通过EntryBoxes向嵌套字典添加键和值,并通过Combobox访问它



是否有可能通过Entrybox添加新的键和值,然后立即通过Combobox访问它,并确保sub-keys的新keyvalues在添加后立即物理保存并附加到dictionary。当我printsub-keys的新增加的keyvalues似乎一切都很好,但dict1本身和Combobox的值没有变化。

代码如下:

from tkinter import *
from tkinter.ttk import Combobox
def add():
dict1[entrybox.get()] = {'name_company': entrybox1.get(), 'country_company': entrybox2.get()}
print(dict1)
dict1 = {"": {"name_company": 0,
"country_company" : 0,},
"Company1": {"name_company": "Company1 LTD",
"country_company": "Germany"}}
window =Tk()
var1=StringVar()
entrybox = Entry(window) #adding main key ("Company1") as an example
entrybox.pack()
entrybox1 = Entry(window) #adding value of sub-key ("name_company")
entrybox1.pack()
entrybox2 = Entry(window) #adding value of sub-key ("country_company")
entrybox2.pack()
combo = Combobox(window, value=list(dict1.keys()), textvariable=dict1[var1.get()])
combo.pack()
button = Button(window, text="Add", command=add)
button.pack()

如果我正确理解了你的问题,我认为下面的代码将为你提供你想要的,或者让你接近你的最终目标。

原始茎的问题在于将条目值存储在某个地方,以便它们可以被检索并作为组合框中的值使用,以及被附加到字典中。

作为解决这个问题的方法,我使用了两个.txt文件,一个用于存储组合框的值,另一个用于存储字典,每次按下添加按钮时,这两个文件都会更新。

Add函数然后从ComboValues.txt文件中检索数据,按字母顺序对它们进行排序,并将它们分配给组合框。我并不是说这是最佳实践,但它应该满足您的需求。

作为旁注,我要提供的一个技巧是在导入模块时避免使用*。它最终可能会变得混乱,尝试只导入您需要的东西,例如。组合框,Stringvar,输入......等

希望这对你有帮助,有任何问题或澄清请联系我。

import os
from tkinter import Entry, StringVar, Tk, Button
from tkinter.ttk import Combobox
Root = Tk()
#set combobox values to empty list
ComboboxValue = []
#String variables for dictionary values
MainKey = StringVar()
SubKey1 = StringVar()
Subkey2 = StringVar()
#Set dictionary values to open list
Dict = {}
#A place to save dictionary key values to read from later
file = open('Combovalues.txt', 'a')
#defining the function for adding entry values to dictionary
def Add():
#Second dictionary used to update original
DictUpdated = {MainKey.get():{'name_company':SubKey1.get(), 'country_company':Subkey2.get()}}
#Method to update original dictionary
Dict.update(DictUpdated)
print(Dict)
#function to update combobox txt file with entry values
with open('ComboValues.txt', 'a') as file:
file.write('n'+(str(SubKey1.get())+'n'+(str(Subkey2.get()))))
file.close
#function to update Dictionary txt file with entry values        
with open('Dictionary.txt', 'a') as file:
file.write('n'+str((Dict)))
file.close
#function to sort through entry values saved to combobox txt file, ready for assigning to combobox
def ValueSource():
with open('ComboValues.txt') as inFile:
ComboboxValue = [line for line in inFile]
ComboboxValue = sorted(ComboboxValue)
return ComboboxValue
#Function to retrieve values from combo txt file and assigned to combobox
def GetUpdatedValues():
Values = ValueSource()
Root.Combobox['values'] = Values
#Class to build GUI   
class GUI ():
def __init__(self, master):
self.master = master
master.EntryMainKey = Entry(Root, textvariable= MainKey).grid(row=0, columnspan=2)
master.EntrySubKey1 = Entry(Root, textvariable= SubKey1).grid(row=1, columnspan=2)
master.EntrySubKey2 = Entry(Root, textvariable= Subkey2).grid(row=2, columnspan=2)
master.AddButton = Button(Root, text='Add', command=Add).grid(row=3, columnspan=2)
master.Combobox = Combobox(Root, state='normal', postcommand=GetUpdatedValues)
master.Combobox['values'] = ComboboxValue
master.Combobox.grid(row=4, columnspan=2)
#Mainloop function    
def main():
GUI(Root)
Root.mainloop()
main()

最新更新