Problem with a Tkinter Menu in Python - menu

I'm actually working on an image editor in Python with Tkinter (I know Python's not the best for image editing but that's for school, I didn't chose) and what I'm trying to do is saving every opened files in another file to add shortcut to the last five ones in the File menu.
The problem is that there's no problem at the start but when I open a new image it can't reconfigure the old shortcuts.
The error seems to indicate that it tries to configure a separator instead of a command. After playing with some print()s it seems that Tkinter see the first five entry then jump the next three and then see all the other.
It's kinda messin' with my head.
Here's the code :
f=open('.recentlyUsed', 'r+')
f.readline() #Skipping the empty first line.
recents=f.read().split('\n')
c=0
recents2=[]
for i in recents: #Deleting the doubles.
if i not in recents2:
recents2+=[i]
recents=recents2[:]
c=0
while fichier.index(c)==c: #Getting the number of files already displayed in
c+=1 #the menu.
print(fichier.entryconfigure(c)) #Test : Printing the entry to know which
c-=7 #Tkinter sees or not.
if c>=0:
fichier.insert_separator(5) #Doesn't insert a separator if there's no file displayed.
if len(recents)>=1: #If there's already a file displayed, juste modifying the label (the command doesn't need to.)
if c>=1:
fichier.entryconfigure(7, label=srecents[-1])
else:
fichier.insert_command(6, command=lambda: openRecent(-1), label=recents[-1])
if len(recents)>=2:
if c>=2:
fichier.entryconfig(8, label=recents[-2])
else:
fichier.insert_command(6, command=lambda: openRecent(-2), label=recents[-2])
if len(recents)>=3:
if c>=3:
fichier.entryconfig(9, label=.recents[-3])
else:
fichier.insert_command(6, command=lambda: openRecent(-3), label=recents[-3])
if len(recents)>=4:
if c>=4:
fichier.entryconfig(10, label=recents[-4])
else:
fichier.insert_command(6, command=lambda: openRecent(-4), label=recents[-4])
if len(recents)>=5:
if c>=5:
fichier.entryconfig(11, label=recents[-5])
else:
fichier.insert_command(6, command=lambda: openRecent(-5), label=recents[-5])
f.close()
And this code returns the following :
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Nouveau'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3071116428nouveau'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Ouvrir...'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056134764open'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Sauver'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'disabled'), 'command': ('command', '', '', '', '3056134892save'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Sauver sous...'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'disabled'), 'command': ('command', '', '', '', '3056134700saveAs'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'background': ('background', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Quitter'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056074828destroy'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Quitter'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056074828destroy'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
#Now I try oening a new image (whether by clicking on one of the shortcut to a recent picture or by the open function a defined in the project).
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Nouveau'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3071116428nouveau'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Ouvrir...'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', <bytecode object at 0x9870a68>), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Sauver'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'disabled'), 'command': ('command', '', '', '', '3056134892save'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Sauver sous...'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'disabled'), 'command': ('command', '', '', '', '3056134700saveAs'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', '/home/aquaj/workspace/projet1/lenna.pgm'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '160341260<lambda>'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', '/home/aquaj/workspace/projet1/boats.pgm'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '160341292<lambda>'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', '/home/aquaj/Projet_Programmation_1/Images/lena2.pgm'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056177964<lambda>'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'background': ('background', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Quitter'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056074828destroy'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Quitter'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056074828destroy'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.1/tkinter/__init__.py", line 1402, in __call__
return self.func(*args)
File "/home/aquaj/workspace/projet1/main.py", line 244, in open
self.recentUse(add=1)
File "/home/aquaj/workspace/projet1/main.py", line 307, in recentUse
self.fichier.entryconfig(9, label=self.recents[-3])
File "/usr/lib/python3.1/tkinter/__init__.py", line 2669, in entryconfigure
return self._configure(('entryconfigure', index), cnf, kw)
File "/usr/lib/python3.1/tkinter/__init__.py", line 1187, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-label"
Where it should display :
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Nouveau'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3071116428nouveau'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Ouvrir...'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', <bytecode object at 0x9870a68>), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Sauver'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'disabled'), 'command': ('command', '', '', '', '3056134892save'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Sauver sous...'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'disabled'), 'command': ('command', '', '', '', '3056134700saveAs'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'background': ('background', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', '/home/aquaj/workspace/projet1/feuille.pgm'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056177964<lambda>'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', '/home/aquaj/workspace/projet1/bateaux.pgm'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056177964<lambda>'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', '/home/aquaj/workspace/projet1/lenna.pgm'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '160341260<lambda>'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', '/home/aquaj/workspace/projet1/boats.pgm'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '160341292<lambda>'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', '/home/aquaj/Projet_Programmation_1/Images/lena2.pgm'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056177964<lambda>'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'background': ('background', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Quitter'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056074828destroy'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
{'foreground': ('foreground', '', '', '', ''), 'accelerator': ('accelerator', '', '', '', ''), 'hidemargin': ('hidemargin', '', '', 0, 0), 'label': ('label', '', '', '', 'Quitter'), 'underline': ('underline', '', '', -1, -1), 'bitmap': ('bitmap', '', '', '', ''), 'columnbreak': ('columnbreak', '', '', 0, 0), 'state': ('state', '', '', <index object at 0x968c230>, 'normal'), 'command': ('command', '', '', '', '3056074828destroy'), 'background': ('background', '', '', '', ''), 'compound': ('compound', 'compound', 'Compound', <index object at 0x9689428>, 'none'), 'font': ('font', '', '', '', ''), 'image': ('image', '', '', '', ''), 'activeforeground': ('activeforeground', '', '', '', ''), 'activebackground': ('activebackground', '', '', '', '')}
So if there's anyone who can see where's the problem comes from... ?

The least complicated solution is to just recreate the whole menu from scratch rather than try to update certain items. It's quick, easy, and almost foolproof.
If you make the design decision to make the recent items the last items on the menu, you can delete just the recent files and append a fresh set. This is very easy since you know the index where the recent files begin unless you have a menu that grows and shrinks for other reasons.
So, don't try to update items in place, just delete and recreate.

That long chain of if's is definitely the wrong way of doing it.
I suggest you first remove all the old recent entries, and add the new ones instead.

Related

Making a list of dict from a list of lists with the list[0] as keys and other lists as values

could you tell me how can I get a list of dicts from that with the a[0] as keys for each dict and a[1:] as values accordingly.
a = [['PORT', 'NAME', 'STATUS', 'VLAN', 'DUPLEX', 'SPEED', 'TYPE', 'FC_MODE'], ['Gi1/0/1', 'S1-P1-01 Cisco_Roo', 'connected', '248', 'a-full', 'a-1000', '10/100/1000BaseTX', ''], ['Gi1/0/2', '', 'notconnect', '121', 'auto', 'auto', '10/100/1000BaseTX', ''], ['Gi1/0/3', '', 'notconnect', '121', 'auto', 'auto', '10/100/1000BaseTX', '']]
I wanna get
[{'PORT' : 'Gi1/0/1',
'NAME' : 'S1-P1-01 Cisco_Roo',
.
.
.
},
{'PORT' : 'Gi1/0/2',
'NAME' : '',
.
.
.
}]
The easiest is probably:
[dict(zip(a[0], x)) for x in a[1:]]
This walks through each element from 1 onwards, and combines it with the first element, converting to a dictionary.
Does this work?
a = [
['PORT', 'NAME', 'STATUS', 'VLAN', 'DUPLEX', 'SPEED', 'TYPE', 'FC_MODE'],
['Gi1/0/1', 'S1-P1-01 Cisco_Roo', 'connected', '248', 'a-full', 'a-1000', '10/100/1000BaseTX', ''],
['Gi1/0/2', '', 'notconnect', '121', 'auto', 'auto', '10/100/1000BaseTX', ''],
['Gi1/0/3', '', 'notconnect', '121', 'auto', 'auto', '10/100/1000BaseTX', '']
]
import pandas as pd
pd.DataFrame(a[1:],columns=a[0]).to_dict(orient="records")
Creates this output.
[{'PORT': 'Gi1/0/1',
'NAME': 'S1-P1-01 Cisco_Roo',
'STATUS': 'connected',
'VLAN': '248',
'DUPLEX': 'a-full',
'SPEED': 'a-1000',
'TYPE': '10/100/1000BaseTX',
'FC_MODE': ''},
{'PORT': 'Gi1/0/2',
'NAME': '',
'STATUS': 'notconnect',
'VLAN': '121',
'DUPLEX': 'auto',
'SPEED': 'auto',
'TYPE': '10/100/1000BaseTX',
'FC_MODE': ''},
{'PORT': 'Gi1/0/3',
'NAME': '',
'STATUS': 'notconnect',
'VLAN': '121',
'DUPLEX': 'auto',
'SPEED': 'auto',
'TYPE': '10/100/1000BaseTX',
'FC_MODE': ''}]

How do I index through each character in a text file?

I have this file:
01000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
1000000000
01100000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
1000000000
I want to iterate through each character and basically put them in a 2d list, where the inside list contains each character, and the outside list contains the list with each character and ends at the \n\n character, so here I will have a list of 2 by 8*8+10. The problem is after each line is the \n character, which it reads as a character too, and also, I've tried doing it with the read() function, which iterates through my list, but for some reason it gets how many characters I give as a parameter, not the index of them. It also starts with the '' character, I guess it adds up how many items I give as a parameter, and puts the different characters in the next list.
Here is my code:
lines, columns=8, 8
bias=10
x=[[0 for i in range(lines*columns)] for j in range(patternCount)]
with open("sabloane.txt", "r") as file:
for pattern in range(patternCount):
for pixel in range(lines*columns+bias):
x[sablon][pixel]=file.read(pattern*patternCount+pixel)
print(x)
It returns:
[['', '0', '10', '000', '00\n0', '00000', '00\n000', '00000\n0', '0000000\n', '00000000\n', '00000000\n0', '0000000\n000', '00000\n100000', '0000\n\n0110000', '0\n00000000\n000', '00000\n00000000\n', '00000000\n0000000', '0\n00000000\n000000', '00\n1000000000\n\n', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']]
Don't make it complicated.
# Assume you got the file string to variable string
outer = []
items = string.split('\n\n')
for item in items:
inner = []
temp = ''.join(item.split('\n'))
for c in temp:
inner.append(c)
outer.append(inner)
Well, for anybody interested to my former comment about getting the Nth element from a file, I just edited #Hanxi Fu's code, so that I put the elements from the first N-1 lines to a matrix, and the other elements from the last line to another matrix.
x=[[0 for i in range(lines*columns)] for j in range(patternCount)]
y=[[0 for i in range(bias)] for j in range(patternCount)]
with open("sabloane.txt", "r") as file:
stringFile=file.read()
patterns=stringFile.split("\n\n")
patterns.pop(patternCount)
for i in range(patternCount):
pixel="".join(patterns[i].split("\n"))
for j in range(lines*columns):
x[i][j]=pixel[j]
for j in range(bias):
y[i][j]=pixel[lines*columns+j]

TypeError: string indices must be integers - json

I want get value (abc.com/p/B3N) from this json :
{'id': 123456, 'parent_id': 0, 'number': '23856', 'order_key': 'abc', 'created_via': 'checkout', 'version': '3.6.4', 'status': 'processing', 'currency': 'USD', 'date_created': '2019-10-05T13:18:49', 'date_created_gmt': '2019-10-05T13:18:49', 'date_modified': '2019-10-05T13:19:20', 'date_modified_gmt': '2019-10-05T13:19:20', 'discount_total': '0.00', 'discount_tax': '0.00', 'shipping_total': '0.00', 'shipping_tax': '0.00', 'cart_tax': '0.00', 'total': '0.40', 'total_tax': '0.00', 'prices_include_tax': False, 'customer_id': 0, 'customer_ip_address': '111.101.111.111', 'customer_user_agent': 'Mozilla/5.0 (Linux; Android 8.0.0; SAMSUNG SM-J337P) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/10.1 Chrome/71.0.3578.99 Mobile Safari/537.36', 'customer_note': '', 'billing': {'first_name': '', 'last_name': '', 'company': '', 'address_1': '', 'address_2': '', 'city': '', 'state': '', 'postcode': '', 'country': '', 'email': 'abc#gmail.com', 'phone': ''}, 'shipping': {'first_name': '', 'last_name': '', 'company': '', 'address_1': '', 'address_2': '', 'city': '', 'state': '', 'postcode': '', 'country': ''}, 'payment_method': 'paypal', 'payment_method_title': 'PayPal', 'transaction_id': '851R', 'date_paid': '2019-10-05T13:19:20', 'date_paid_gmt': '2019-10-05T13:19:20', 'date_completed': None, 'date_completed_gmt': None, 'cart_hash': '0675772a1e', 'meta_data': [{'id': 123456, 'key': 'is_vat_exempt', 'value': 'no'}, {'id': 123456, 'key': 'Payment type', 'value': 'instant'}, {'id': 274929, 'key': '_paypal_status', 'value': 'completed'}, {'id': 123456, 'key': 'PayPal Transaction Fee', 'value': '0.32'}], 'line_items': [{'id': 10927, 'name': 'Jeans', 'product_id': 1234, 'variation_id': 0, 'quantity': 1, 'tax_class': '', 'subtotal': '0.10', 'subtotal_tax': '0.00', 'total': '0.10', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 100000, 'key': '', 'value': 'Views $0.00 × 500'}, {'id': 100001, 'key': '', 'value': 'Worldwide'}, {'id': 100002, 'key': '', 'value': 'abc.com/p/B3N'}, {'id': 100003, 'key': '', 'value': '17'}], 'sku': '', 'price': 0.1}], 'tax_lines': [], 'shipping_lines': [], 'fee_lines': [{'id': 10928, 'name': 'PayPal Fee (Free Fee for order over $5)', 'tax_class': '0', 'tax_status': 'taxable', 'amount': '0.3', 'total': '0.30', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 122543, 'key': '_legacy_fee_key', 'value': 'paypal-fee'}]}], 'coupon_lines': [], 'refunds': [], '_links': {'self': [{'href': 'abc.com'}], 'collection': [{'href': 'abc.com'}]}}
this is my code
m = (wcapi.get(order + ordernumber).json())
n = json.dumps(m)
o = json.loads(n)
for i in o:
if i['id'] == '100002':
print(i['value'])
break
and i got this error :
if i['id'] == '100002':
TypeError: string indices must be integers
i have searched others topics but ... can't. thanks for help me!
When you do for i in o and o is a dictionary, the for loop iterates over the keys in o - which are strings in your case. Hence the error. i is a string.
To get the key you need to know the exact structure of o.
I'm gonna give you some examples:
o['id'] # 123456
o['billing']['email'] # "abc#gmai.com"
Now to get the value you want:
first_line_items_meta = o['line_items'][0]['metadata']
for item in first_line_items_meta:
if item['id'] == 100002:
print(item['value']) # "abc.com/p/B3N"

Show the sum for multiple items for a dynamic numbers

I've a data dictionary extract from my project, i want to pickup items for example at the 2nd range, from different lists as value of dictionary, i tried a method suggested by a someone in this forum "Thank u for every help!" this time i want to select items from every key start with 345##### what ever the rest #### (Note: that selected items will be at max 20) , the loop start to select item and creat a sum if did't find the key at the next position it will hold the sum, and insert it at QtableWidget cell, i tried a different way :/ any suggestion please.
import sys
from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem)
from PyQt5.QtGui import QBrush, QColor
from PyQt5 import QtCore
#from FixalSolutions_build_1_3 import Ui_MainWindow
data = {'1111':['Capital 1','145321','94565','','','','74651','','','24651','','',''], '1112':['Capital 2','65115','','6149','6645','555641','','','','41245','98416','',''], '1113':['Capital 3','544453','','45345453','','555641','434556','','453453','','98416','','453453'], '1114':['Capital 4','7144453','','345453','3155485','','894556','','156453','326149','98416','',''], '1121':['Capital 5','87676','','534553','466149','','','95436','','','76745','','74568',''], '1122':['Capital 6','45645','','','47679','17345','555641','345256','','','412045','98416','',''], '1123':['Capital 7','4534453','74345','','466149','44645','1','98656','12564','','412045','98416','',''], '1124':['Capital 8','9784534','','','1326149','14553','555641','','45345','445345','412045','98416','',''], '1161': ['RAN SC', '', '', '', '32412.8', '', '32412.8', '', '', '', '', '', ''], '1162': ['Compte', '', '49850.22', '', '', '', '49850.22', '', '', '', '49850.22', '', '49850.22'], '1169': ['RAN SD', '22744.59', '', '', '', '22744.59', '', '', '', '22744.59', '', '22744.59', ''], '3455': ['item 1', '9880.4', '', '', '', '9880.4', '', '', '', '9880.4', '', '9880.4', ''], '34550200': [ 'item 2', '681.66', '', '', '', '681.66', '', '', '', '681.66', '', '681.66', ''], '34552010': [ 'item 3', '1083.87', '', '', '', '1083.87', '', '', '', '1083.87', '', '1083.87', ''], '34552020': [ 'item 4', '', '', '38026.61', '34080.67', '3945.94', '', '', '', '', '', '', ''], '34552140': [ 'item 5', '17358.79', '', '', '', '17358.79', '', '', '', '17358.79', '', '17358.79', ''], '34552200': [ 'item 6', '', '28037.7', '', '', '', '28037.7', '', '', '', '28037.7', '', '28037.7']}
class Table(QWidget):
def __init__(self, *args, parent=None):
super().__init__()
self.data = data
self.setuptUI()
def setuptUI(self):
self.setWindowTitle("QTableWidgetItem")
self.resize(1200, 800)
conLayout = QHBoxLayout()
self.tableWidget =QTableWidget(self)
self.tableWidget.setRowCount(55)
self.tableWidget.setColumnCount(14)
conLayout.addWidget(self.tableWidget)
def setdata(self, k, v, n, m):
i= 1
l = str(k*10)
item = 0
while i < i+1
if l in self.data
s = self.data.get(str(int(l)+i))[v]
i= i+1
print(item)
item = item + (int(s) if s else 0)
else:
break
self.tableWidget_Bilan.setItem(n, m, QTableWidgetItem(str(item)))
if __name__ == '__main__':
app = QApplication(sys.argv)
windows = Table(data)
windows.setdata(k="345", v=5, n=25, m=4
windows.show()
sys.exit(app.exec_())
Try it:
import sys
from PyQt5.QtWidgets import (QWidget, QTableWidget, QHBoxLayout,
QApplication, QTableWidgetItem)
from PyQt5.QtGui import QBrush, QColor
from PyQt5 import QtCore
#from FixalSolutions_build_1_3 import Ui_MainWindow
class Table(QWidget):
def __init__(self, data):
super().__init__()
self.data = data
self.setuptUI()
def setuptUI(self):
self.setWindowTitle("QTableWidgetItem")
self.resize(1200, 600)
self.tableWidget =QTableWidget(self)
self.tableWidget.setRowCount(55)
self.tableWidget.setColumnCount(14)
conLayout = QHBoxLayout(self) # + self
conLayout.addWidget(self.tableWidget)
def setdata(self, k, v, r, c):
item = 0
for kd, vd in self.data.items():
if kd[:len(k)] == k:
s = vd[v]
print("{:<10} -> `{: 10.2f}`".format(kd, float(s) if s else 0))
item = item + (float(s) if s else 0)
self.tableWidget.setItem(r, c, QTableWidgetItem(str(item)))
data = {'1111':['Capital 1','145321','94565','','','','74651','','','24651','','',''],
'1112':['Capital 2','65115','','6149','6645','555641','','','','41245','98416','',''],
'1113':['Capital 3','544453','','45345453','','555641','434556','','453453','','98416','','453453'],
'1114':['Capital 4','7144453','','345453','3155485','','894556','','156453','326149','98416','',''],
'1121':['Capital 5','87676','','534553','466149','','','95436','','','76745','','74568',''],
'1122':['Capital 6','45645','','','47679','17345','555641','345256','','','412045','98416','',''],
'1123':['Capital 7','4534453','74345','','466149','44645','1','98656','12564','','412045','98416','',''],
'1124':['Capital 8','9784534','','','1326149','14553','555641','','45345','445345','412045','98416','',''],
'1161': ['RAN SC', '', '', '', '32412.8', '', '32412.8', '', '', '', '', '', ''],
'1162': ['Compte', '', '49850.22', '', '', '', '49850.22', '', '', '', '49850.22', '', '49850.22'],
'1169': ['RAN SD', '22744.59', '', '', '', '22744.59', '', '', '', '22744.59', '', '22744.59', ''],
'3455': ['item 1', '9880.4', '', '', '', '9880.4', '', '', '', '9880.4', '', '9880.4', ''],
'34550200': [ 'item 2', '681.66', '', '', '', '681.66', '', '', '', '681.66', '', '681.66', ''],
'34552010': [ 'item 3', '1083.87', '', '', '', '1083.87', '', '', '', '1083.87', '', '1083.87', ''],
'34552020': [ 'item 4', '', '', '38026.61', '34080.67', '3945.94', '', '', '', '', '', '', ''],
'34552140': [ 'item 5', '17358.79', '', '', '', '17358.79', '', '', '', '17358.79', '', '17358.79', ''],
'34552200': [ 'item 6', '', '28037.7', '', '', '', '28037.7', '', '', '', '28037.7', '', '28037.7']}
if __name__ == '__main__':
app = QApplication(sys.argv)
windows = Table(data)
windows.setdata(k="345", v=5, r=25, c=4)
windows.show()
sys.exit(app.exec_())

Chart/Graph/Monitoring data I get from an api in .json format

I am looking for the best tool to automate the creating of graphs from QOS information I get from a videoconferencing service's API in .json format
I initially wanted to monitor this data but creating graphs would be good enough as I need to hit the API and compare QOS statistics for each meeting.
Is there a way to do this? I Like Python and thought about grafana but cannot find a way to do it.
This is an example of the data I get for each call. There are plenty of "user_qos" but it would be too long to paste here and I guess it can be understood as I pasted it.
I guess I should concentrate on the "user_qos" part and previously edit the file through Python.
{'next_page_token': '',
'page_count': 1,
'page_size': 10,
'participants': [{'camera': 'Camera',
'connection_type': 'UDP',
'data_center': 'PP (Botom)',
'device': 'Rooms',
'domain': '',
'harddisk_id': '',
'id': 'xxxxxxxxxxxxxxxx',
'ip_address': 'xxx.xxx.xxx.xxx',
'join_time': '2019-02-15T18:25:29Z',
'leave_time': '2019-02-15T19:20:19Z',
'location': 'Some Place (Earth)',
'mac_addr': '',
'microphone': 'USB audio CODEC',
'network_type': 'Wired',
'pc_name': '',
'recording': False,
'share_application': False,
'share_desktop': False,
'share_whiteboard': False,
'speaker': 'Built-in Output (Headphones)',
'user_id': '22222222',
'user_name': 'room',
'user_qos': [{'as_input': {'avg_loss': '0.0%',
'bitrate': '39 kbps',
'frame_rate': '8 fps',
'jitter': '7 ms',
'latency': '20 ms',
'max_loss': '0.0%',
'resolution': '1440*900'},
'as_output': {'avg_loss': '',
'bitrate': '',
'frame_rate': '',
'jitter': '',
'latency': '',
'max_loss': '',
'resolution': ''},
'audio_input': {'avg_loss': '',
'bitrate': '',
'jitter': '',
'latency': '',
'max_loss': ''},
'audio_output': {'avg_loss': '0.0%',
'bitrate': '85 kbps',
'jitter': '11 ms',
'latency': '31 ms',
'max_loss': '0.0%'},
'cpu_usage': {'system_max_cpu_usage': '69%',
'avg_cpu_usage': '19%',
'max_cpu_usage': '22%',
'min_cpu_usage': '15%'},
'date_time': '2019-02-15T18:27:00Z',
'video_input': {'avg_loss': '0.1%',
'bitrate': '0 kbps',
'frame_rate': '',
'jitter': '8 ms',
'latency': '22 ms',
'max_loss': '4.1%',
'resolution': ''},
'video_output': {'avg_loss': '0.0%',
'bitrate': '140 kbps',
'frame_rate': '6 fps',
'jitter': '11 ms',
'latency': '28 ms',
'max_loss': '0.0%',
'resolution': '320*180'}},
{'as_input': {'avg_loss': '0.0%',
'bitrate': '39 kbps',
'frame_rate': '8 fps',
'jitter': '7 ms',
'latency': '20 ms',
'max_loss': '0.0%',
'resolution': '1440*900'},
'as_output': {'avg_loss': '',
'bitrate': '',
'frame_rate': '',
'jitter': '',
'latency': '',
'max_loss': '',
'resolution': ''},
'audio_input': {'avg_loss': '',
'bitrate': '',
'jitter': '',
'latency': '',
'max_loss': ''},
'audio_output': {'avg_loss': '0.0%',
'bitrate': '85 kbps',
'jitter': '11 ms',
'latency': '31 ms',
'max_loss': '0.0%'},
'cpu_usage': {'system_max_cpu_usage': '69%',
'avg_cpu_usage': '19%',
'max_cpu_usage': '22%',
'min_cpu_usage': '15%'},
'date_time': '2019-02-15T18:27:00Z',
'video_input': {'avg_loss': '0.1%',
'bitrate': '0 kbps',
'frame_rate': '',
'jitter': '8 ms',
'latency': '22 ms',
'max_loss': '4.1%',
'resolution': ''},
'video_output': {'avg_loss': '0.0%',
'bitrate': '140 kbps',
'frame_rate': '6 fps',
'jitter': '11 ms',
'latency': '28 ms',
'max_loss': '0.0%',
'resolution': '320*180'}}],
'version': '4.1.33237.0924'}],
'total_records': 2}

Resources