会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132441个问题
Python 全系列/第一阶段:Python入门/面向对象 31981楼
JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:使用RabbitMQ实现大广告异步缓存 31982楼
Python 全系列/第五阶段:数据库编程/mysql的使用 31984楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 31987楼

老师在def openfile之后,运行并选择打开文件后,会报这个错误是怎么回事呢?

下面两种写法都会报错

image.png

"""开发记事本软件的菜单"""

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


class Application(Frame):

    def __init__(self, master=None):
        super().__init__(master)        # super()代表的是父类的定义,而不是父类对象
        self.master = master
        self.textpad = None             # textpad表示Text文本框对象
        self.pack()
        self.createWidget()

    def createWidget(self):
        # 创建主菜单栏
        menubar = Menu(root)

        # 创建子菜单
        menuFile = Menu(menubar)
        menuEdit = Menu(menubar)
        menuHelp = Menu(menubar)

        # 将子菜单加入主菜单栏
        menubar.add_cascade(label='文件(F)', menu=menuFile)
        menubar.add_cascade(label='编辑(E)', menu=menuEdit)
        menubar.add_cascade(label='帮助(H)', menu=menuHelp)

        # 添加菜单项
        menuFile.add_command(label='新建', accelerator='ctrl+n', command=self.test)
        menuFile.add_command(label='打开', accelerator='ctrl+o', command=self.openfile)
        menuFile.add_command(label='保存', accelerator='ctrl+s', command=self.test)
        menuFile.add_separator()  # 添加分割线
        menuFile.add_command(label='退出', accelerator='ctrl+q', command=self.test)

        # 将主菜单加到根窗口
        root['menu'] = menubar

        # 文本编辑区
        self.textpad = Text(root, width=65, height=30)
        self.textpad.pack()

        # 创建上下菜单
        self.contextMenu = Menu(root)
        self.contextMenu.add_command(label='背景颜色', command=self.test)

        # 右键绑定事件
        root.bind('<Button-3>', self.createContextMenu)

    def openfile(self):
        # self.textpad.delete("1.0", "end")  # 把text控件中所有的内容清空
        # with askopenfile(title="打开文本文件") as f:
        #     self.textpad.insert(INSERT, f.read())
        #     self.filename = f.name
        try:
            # 以utf-8编码打开
            with open(askopenfilename(), encoding='utf-8') as f:
                self.textpad.insert(INSERT. f.read())
                self.filename = f.name
                print(f.read())
        except UnicodeDecodeError:
            # 以GBK编码打开
            with open(askopenfilename()) as f:
                print(f.read())

    def test(self):
        pass

    def createContextMenu(self, event):
        self.contextMenu.post(event.x_root, event.y_root)


if __name__ == '__main__':
    root = Tk()
    root.geometry("450x300+200+300")
    root.title("百战程序员的简易记事本")
    app = Application(master=root)
    root.mainloop()


Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 31988楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 31989楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 31990楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 31991楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637