这是一个非常基本的问题。我正在阅读《 Python编程》一书,并按照教程进行学习。但是这本书是基于Windows的,而且我在Mac中使用,结果似乎有所不同。
代码是这样的:
from tkinter import *
from tkinter.messagebox import showerror
import shelve
shelvename='class-shelve'
fieldnames=('name','age','job','pay')
def makeWidgets():
global entries
window=Tk()
window.title('People Shelve')
form=Frame(window)
form.pack()
entries={}
for (ix,label) in enumerate(('key',)+fieldnames):
lab=Label(form,text=label)
ent=Entry(form)
lab.grid(row=ix,column=0)
lab.grid(row=ix,column=1)
entries[label]=ent
Button(window,text="Fetch",command=fetchRecord).pack(side=LEFT)
Button(window,text="Update",command=updateRecord).pack(side=LEFT)
Button(window,text="Quit",command=window.quit).pack(side=RIGHT)
return window
def fetchRecord():
key=entries['key'].get()
try:
record=db[key]
except:
showerror(title='Error',message='No such key!')
else:
for field in fieldnames:
entries[field].delete(0,END)
entries[field].insert(0,repr(getattr(record,field)))
def updateRecord():
key=entries['key'].get()
if key in db:
record=db[key]
else:
from person import Person
record=Person(name='?',age='?')
for field in fieldnames:
setattr(record, field, eval(entries[field].get()))
db[key]=record
db=shelve.open(shelvename)
window=makeWidgets()
window.mainloop()
db.close()
这本书说结果应该像这样:
但是它确实在我的Mac中是这样的:
我该如何解决?