please help bring this file data.
code:
import tkinter
import shelve
def fileOpen():
print('start output dictonary "db"', end = '\n')
try:
db = shelve.open('data', 'r')
except:
print('invalid filename. try again')
else:
for record in db:
print(record, ':: ', end = '\n')
print('\ttype:\t', db[record].type, end = '\n')
print('\tnumber:\t', db[record].number, end = '\n')
print('\tvideo:\t', db[record].video, end = '\n')
print('\taudio:\t', db[record].audio, end = '\n')
print('=================')
db.close()
def recAction(event, id, **args):
print('ID: ', id, end = '\n')
for arg in args:
print(arg, '---', args[arg], end = '\n')
db = shelve.open('data')
try:
db[id] = args
except:
tkinter.messagebox.showerror("Error", "invalid record. try again")
else:
db.close()
tkinter.messagebox.showinfo("Complete", "Record complete")
print('OK. now output dictonary "db"')
fileOpen()
root = tkinter.Tk()
button = tkinter.Button(root, text = 'Send', height = 20, width = 20, relief = 'raised', cursor = 'hand1', font = ('times', 14, 'bold'))
button.bind('<Button-1>', lambda event: recAction(event, '1', type = 'dvd', number = '13', video = 'srat wars', audio = 'soundtrack'))
button.pack()
root.mainloop()
here functions recAction () is a write data to a file data. then function fileOpen () is output to the screen. the problem is that when data is output an error message:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args) File "C:\Python33\projects\DVD_LIST\p3_dvd_list_shelve_3d_class_edit_menubar\q.py", line 40, in <lambda>
button.bind('<Button-1>', lambda event: recAction(event, '1', type = 'dvd', number = '13', video = 'srat wars', audio = 'soundtrack')) File "C:\Python33\projects\DVD_LIST\p3_dvd_list_shelve_3d_class_edit_menubar\q.py", line 36, in recAction
fileOpen() File "C:\Python33\projects\DVD_LIST\p3_dvd_list_shelve_3d_class_edit_menubar\q.py", line 13, in fileOpen
print('\ttype:\t', db[record].type, end = '\n') AttributeError: 'dict' object has no attribute 'type'
In the for record in db
loop you're iterating a dictionary.
Dictionaries can be iterated over in a better way.
for record_key, record_value in db.items():
print(record_key, ':: ', end = '\n')
print('\ttype:\t', record_value['type'], end = '\n')
print('\tnumber:\t', record_value['number'], end = '\n')
print('\tvideo:\t', record_value['video'], end = '\n')
print('\taudio:\t', record_value['audio'], end = '\n')
print('=================')
The error message indicates db[record]
is dictionary as well.