python - 通过 EntryBoxes 向嵌套字典添加键和值,并通过 Combobox 访问它
问题描述
是否可以通过添加新的键和值Entrybox
,然后立即访问它,Combobox
同时确保新的key
和values
of被物理保存并在添加后sub-keys
附加到右侧。dictionary
当我print
新添加key
的一切似乎都很好,但其本身和values
价值sub-keys
没有变化。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 文件,一个用于存储 Combobox 的值,另一个用于存储字典,每次按下添加按钮时都会更新。
然后 Add 函数从 ComboValues.txt 文件中检索数据,将它们按字母顺序排序并将它们分配给 Combobox。我并不是说这是最佳实践,但它应该满足您的要求。
在旁注中,我提供的一个提示是在导入模块时避免使用 *。它最终可能会变得一团糟,试着只导入你需要的东西,例如。Combobox、Stringvar、Entry ......等。
希望这会有所帮助,任何问题或澄清都会打我。
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()
推荐阅读
- dialogflow-es - 在 Dialogflow 中使用 $Knowledge.Answer[2] 或 $Knowledge.Answer[3]
- r - 从文本文件中读取 R 表时的奇怪行为
- hyperledger-fabric - 有时启动网络或升级网络时使用 Hyperledger Composer 与 HyperLedger Fabric(分布式)交互失败
- javascript - 表单提交后删除按钮样式
- docker - docker compose nginx代理不重定向
- neo4j - Neo4j 查询错误地返回 null
- sql - SQL 左连接与 JOIN 条件中的过滤器与 WHERE 子句中的过滤器
- javascript - 如何在 jQuery 集合中选择元素
- php - 在 codeigniter 项目中使用 Ruler 库
- laravel - 无法从数据表中删除小时和分钟