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

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


from tkinter.filedialog import *
from tkinter.colorchooser 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)

        #print(menubar)
        #创建子菜单
        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.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.config(menu=menubar)
        root["menu"]= menubar


        #添加文本框
        self.textpad=Text(root)
        self.textpad.pack()

        #创建上下菜单
        self.contextMenu=Menu(root)
        self.contextMenu.add_command(label="背景颜色",command=self.openAskColor)
        #鼠标右键绑定事件
        root.bind("<Button-3>",self.createContextMenu)#通过鼠标右键单击组件而弹出的菜单,需要通过调用post显示鼠标的坐标来显示位置,还有定义方法显示

    def newfile(self):
        self.textpad.delete("1.0","end")
        self.filename=asksaveasfile(title="另存为",initialfile="未命名.txt",filetypes=[("文本文档","*.txt")],
                                    defaultextension=".txt")

        self.savefile()

    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



    def savefile(self):
        with open(self.filename,"w") as f:
            c=self.textpad.get(1.0,END)#获取textpad内的所有内容,
            f.write(c)#写到打开的文件中

    def exit(self):
        root.quit()

    def openAskColor(self):
        s1=askcolor(color="red",title="选择背景色")
        self.textpad.config(bg=s1[1])
    def createContextMenu(self,event):
        #菜单在鼠标右键单击的坐标处显示
        self.contextMenu.post(event.x_root,event.y_root)




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

点击newfile保存后报错是这个,不知道哪里出问题

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/Test/Desktop/study/my26.py", line 54, in newfile
    self.savefile()
  File "C:/Users/Test/Desktop/study/my26.py", line 65, in savefile
    with open(self.filename,"w") as f:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 4291楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 4292楼
JAVA 全系列/第二十二阶段:分布式医疗云平台/系统管理前后端开发(旧) 4293楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 4294楼
Python 全系列/第一阶段:Python入门/编程基本概念 4295楼
JAVA 全系列/第十一阶段:分布式RPC调用和分布式文件存储/FastDFS 4296楼
JAVA 全系列/第八阶段:Linux入门到实战/Linux(旧) 4297楼
JAVA 全系列/第十四阶段:分布式文件存储与数据缓存/MongoDB 4298楼
Python 全系列/第一阶段:Python入门/面向对象 4301楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 4304楼
JAVA 全系列/第六阶段:JavaWeb开发/Servlet技术详解 4305楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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