会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134051个问题

image.png

a.txt

老师,我在打开这个文件时,如果文本中出现了中文,就会报错,这个该如何解决呢?

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


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

class Application(Frame):

    def __init__(self,master=None):
        super().__init__(master)

        self.master = master
        self.textpad = None
        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=50,height=30)
        self.textpad.pack()

    def openfile(self):
        with askopenfile(title="打开文本文件") as f:
            print(f.read())


    def test(self):
        pass


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


Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1576楼
Python全系列/第二阶段:Python 深入与提高/文件处理 1577楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1578楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1579楼
Python全系列/第二阶段:Python 深入与提高/文件处理 1581楼
Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 1582楼
Python全系列/第二阶段:Python 深入与提高/文件处理 1583楼
Python全系列/第二阶段:Python 深入与提高/坦克大战 1584楼
Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 1585楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1587楼
Python全系列/第二阶段:Python 深入与提高/文件处理 1588楼

新建文本文档.zip

计算机怎么写计算正负,求指导!

Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1589楼
Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1590楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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