self.textpad后面所带相关函数都用不了。

用python写一个记事本,打开文件,出现错误

记事本项目

from tkinter.colorchooser import *
from tkinter .filedialog import *

class Application(Frame):
def init(self, master=None):
super().__init__(master) # super()代表父类的定义,而不是父类对象
self .master = master
self .textpad = None # textpad表示Text文本框对象
self .filename = None # filename表示打开文件的名字
self .contextMenu = None # contextMenu表示上下文菜单对象
self .pack()
self .createWidget()

def createWidget(self):
    # 创建主菜单
    menuber = Menu(root)
    # 创建子菜单
    menuFile = Menu(menuber)
    menuEdit = Menu(menuber)
    menuHelp = Menu(menuber)
    # 将子菜单加入到主菜单栏
    menuber .add_cascade(label='文件(F)', menu=menuFile)
    menuber .add_cascade(label='编辑(E)', menu=menuEdit)
    menuber .add_cascade(label='帮助(H)', menu=menuHelp)
    # 添加菜单项
    menuFile .add_command(label='新建', accelerator='ctrl+n', command=self.newfile)
    menuFile .add_command(label='打开', accelerator='ctrl+o', command=self.openfile)
    menuFile .add_command(label='保存', accelerator='ctrl+s', command=self.savefile)
    menuFile .add_separator()  # 添加分割线
    menuFile .add_command(label='退出', accelerator='ctrl+q', command=self.exit)
    # 将主菜单加到根窗口
    root['menu'] = menuber
    # 添加快捷键处理
    root .bind('<Control-n>', lambda event: self .newfile())
    root .bind('<Control-o>', lambda event: self .openfile())
    root .bind('<Control-s>', lambda event: self .savefile())
    root .bind('<Control-q>', lambda event: self .exit())
    # 文本编辑区
    self .textpad = Text(root, width=50, height=30).pack()
    # 上下菜单
    self .contextMenu = Menu(root)
    self .contextMenu .add_command(label='背景颜色', command=self.openAskColor)
    # 为右键绑定事件
    root .bind('<Button-3>', self .createContextMenu)

def newfile(self):
    pass


def openfile(self):
    self .textpad .delete(1.0, END)
    with askopenfile(title='文本文件')as f:
        #print(f .read())   # 在控制界面打印
        self .textpad .insert(INSERT, f .read())
        self .filename = f .name

def savefile(self):
    with open(self .filename, 'w')as f:
        c = self .textpad.get(1.0, END)
        f .write(c)

def exit(self):
    root .quit()

def createContextMenu(self,event):
    # 菜单在鼠标右键的坐标处显示
    self .contextMenu.post(event.x_root, event.y_root)

def openAskColor(self):
    s1 = askcolor(title='选择背景色', color='red')
    self .textpad.config(bg=s1[1])

if name == '__main__':
root = Tk()
root .geometry('450x300+200+300')
root .title('记事本')
app = Application(master=root)
root .mainloop()

出现报错
File "D:/Users/86135/PycharmProjects/gui2/mypy05.py", line 53, in openfile
self .textpad .delete(1.0, END)
AttributeError: 'NoneType' object has no attribute 'delete'
初学者刚刚学python,查找过百度,不知道怎么解决

http://www.voidcn.com/article/p-pzmjknkz-btv.html

你在学习高淇的python视频吗?我这两天也看到这里了。刚才试着将“# 添加菜单项”下的command那里的

command=self.openfile

改为

command=(lambda: self.openfile())

就莫名其妙的好了,然后再改回

command=self.openfile

也没有任何问题了