Basically I have 8 entries I want to create in a loop and store in a dictionary, with a string as key:
class foo:
entries={}
keys=[#some keys are here, type string]
#do other stuff
def create_entries(self):
for key in keys:
entries[key]=ttk.Entry(self.frame,text='sometext')
#other stuff going on
def assign(self):
f=open('file.name','r').read()
#do some fancy slicing to get the strings for the entries
for key in keys:
entries[key].insert(0,string)
now here it fails, stating that 'NoneType' object has no attribute 'insert'.
I guess this is because I have declared entries as an empty dictionary.
But if I declare it like this: entries={'KEY':ttk.Entry}
still states there is no insert for 'NoneType'. And if I declare it like entries={'KEY':ttk,Entry()}
it initalises an empty toplayer on start, but if I come to load my entries, it tells me again, there is no insert for 'NoneType'.
So I am kind of lost right now.. is it even possible to initialise entries into a dictionary and later on insert some text in them? Or do I have to stick with each entry as a "individual variable"?
minimum working example:
If I delete the prints in the read-function and uncomment the inserts, it tells me:
self.entries[key].insert(0,s)
AttributeError: 'NoneType' object has no attribute 'insert'
import tkinter as tk
from tkinter import ttk
f = open('testfile.test', 'w')
f.write('A=non-relevant\nB=SomeFunnyStuff\nZ=MySecretCode:9128\n')
f.close()
class foo:
main = tk.Tk
frame = ttk.LabelFrame
keys = ['A','B','Z']
entries={}
labels={}
def read(self):
f = open('testfile.test','r').read()
for key in self.keys:
first=key+'='
if key != 'Z':
i = self.keys.index(key)+1
last='\n'+self.keys[i]+'='
s=f.split(first)[1].split(last)[0]
print(s)#self.entries[key].insert(0,s)
else:
s=f.split(first)[1]
print(s)#self.entries[key].insert(0,s)
def _quit(self):
self.main.quit()
self.main.destroy()
exit()
def run(self):
self.main.mainloop()
def __init__(self):
#init window
self.main = tk.Tk()
self.main.title('Test')
self.frame = ttk.LabelFrame(self.main, text='Try it').grid(column=0,row=0)
#init entries & labels
c,r=0,0
for key in self.keys:
s = key+'='
self.labels[key] = ttk.Label(self.frame, text=s).grid(column=c,row=r)
c+=1
self.entries[key] = ttk.Entry(self.frame,text=s).grid(column=c,row=r)
r+=1
c-=1
self.button = ttk.Button(self.frame, text='close',command=lambda:self._quit()).grid(column=0,row=r)
self.read()
print(self.entries.values())
t = foo()
t.run()