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

老师看看哪里出了问题

from tkinter import *
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.test)
        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, heigth=30)
        self.textpad.pack()
        # 创建上下菜单
        self.contextMenu = Menu(root)
        self.contextMenu.add_command(label="背景颜色", command=self.test)
        # 右键绑定事件
        root.bind("<Button-3>", self.createContextMenu)

    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()

image.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 16847楼
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 16851楼

一、

/**
 * 对象流将list存储(序列化)到文件中
 */
public void savePersonList() {
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/a.txt")));
        for (int i = 0; i < this.list.size(); i++) {
            oos.writeObject(this.list.get(i));
        }
        oos.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

二、

/**
 * 获取文件的对象信息(对象的反序列化)
 */
public void getPersonList() {
    //声明流
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    try{
        //实例化流对象
        fis = new FileInputStream("d:/a.txt");
        ois = new ObjectInputStream(new BufferedInputStream(fis));
        try{
            while (fis.available()==0){
                Person person = (Person) ois.readObject();
                list.add(person);
            }
        }catch (Exception e){
            System.out.println("获取上次记录成功!");
        }
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try{
            if (fis!=null){
                fis.close();
            }
            if (ois!=null){
                ois.close();
            }
        }catch (Exception E){
            E.printStackTrace();
        }
    }
}

三、问题(程序可以运行成功。实现对象存入文件,并启动程序时读取文件中的对象到list)

遇到的问题:1、代码块二中,while循环里判断条件是(fis.available()==0),我是在网上找的这个判断读取完成的条件。如果条件换成true也可以运行出来,老师可以讲讲区别吗,available方法是什么作用?

2、代码块二中、这里流的关闭顺序不清楚(主要是开启的顺序不知道)

3、老师看看代码有什么需要改进的地方吗?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 16853楼
JAVA 全系列/第八阶段:Linux入门到实战/MybatisPlus 16854楼
WEB前端全系列/第六阶段:Http服务与Ajax模块(旧)/Http服务与Ajax编程 16855楼
WEB前端全系列/第六阶段:Http服务与Ajax模块(旧)/Http服务与Ajax编程 16856楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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