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

from tkinter import *
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      #textpad表示Text文本框对象
        self.filename = None     #filename表示打开文本文件的名字
        self.contextMenu = None   #contextMenu上下文菜单对象
        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.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"] = menubar

        #添加快捷键事件处理
        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)
        self.textpad.pack()

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

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

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

    def openfile(self):
        self.textpad.delete("1.0","end")
        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)
            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("450x300+600+300")
    root.title("简单记事本")
    app = Application(master = root)
    root.mainloop()

老师我每次新建文档时都没有.txt出现,并且打开文件时出现的都不是我输入的文字,并且保存后再次打开没有变化Z1B4F(31WRQ))I%}WE6OV9W.png)RB(}RUBKM%A796U@GNYX4P.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 5926楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 5927楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 5928楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器 5930楼

image.png

Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础 5931楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 5932楼
JAVA 全系列/第十七阶段:微信与支付宝在线支付/支付宝支付 5933楼
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC旧 5935楼
Python 全系列/第四阶段:函数式编程和核心特性/python新特性 5936楼
JAVA 全系列/第十八阶段:亿级高并发电商项目/亿级高并发电商项目(旧) 5937楼

SVW_UW]%3VT5EAH`JD~64N5.png

Y7~EWN1Q43{BUDE4V]JF4BN.p

不是说python整型很大的吗,9**1000可以,9**10000就报错了

Python 全系列/第一阶段:Python入门/编程基本概念 5939楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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