会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132413个问题
Python 全系列/第十八阶段:数据分析-数据可视化/matplotlib(23旧) 736楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 739楼

老师您好!请问我直接运行我代码,然后在text框里写入一些文本内容后直接点击保存时会报一个错:TypeError: expected str, bytes or os.PathLike object, not NoneType,于是我在保存里加了个判断self.filename是否为空,空的话给它一个保存路径,再运行时才能直接保存:

self.filename = 'F:/Python_pycharm/turtleproject/未命名.txt'

有没有方法能点击直接保存时使他自己选择路径,再进行保存。

代码:

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


class Application(Frame):

    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.filename = None
        self.txt1 = 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.newCreateFile)
        menuFile.add_command(label="打开", accelerator="ctrl+o", command=self.openFile)
        menuFile.add_command(label="保存", accelerator="ctrl+s", command=self.saveFile)

        menuEdit.add_command(label="选择背景色", command=self.optionColor)

        root["menu"] = menubar

        self.txt1 = Text(root, width=400, height=300, bg='white')
        self.txt1.pack()


    def newCreateFile(self):
        # 新建文本文档
        self.txt1.delete('1.0', 'end')
        self.filename = asksaveasfilename(title="另存为", initialfile="未命名.txt",
                                      filetypes=[("文本文档", "*.txt")], defaultextension=".txt")
        print(self.filename)
        self.saveFile()

    def openFile(self):
        # 打开文件
        self.txt1.delete('1.0', 'end')
        with askopenfile(title='打开文本文件') as f:
            self.txt1.insert(INSERT, f.read())
            self.filename = f.name
            root.title(self.filename)
            print(f.name)

    def saveFile(self):
        # 保存文件
        if self.filename is not None:
            with open(self.filename, "w") as f:
                c = self.txt1.get(1.0, END)
                f.write(c)
        else:
            self.filename = 'F:/Python_pycharm/turtleproject/未命名.txt'
            c = self.txt1.get(1.0, END)
            with open(self.filename, "w") as f:
                f.write(c)

    def optionColor(self):
        # 选择背景色
        color = askcolor(color='white', title='选择背景色')
        self.txt1.config(bg=color[1])


if __name__ == '__main__':
    root = Tk()
    root.title('新建文本文档.txt  新版记事本')
    root.geometry('800x600+200+100')
    app = Application(master=root)
    root.mainloop()


Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 740楼
JAVA 全系列/第十一阶段:消息中间件与高并发处理/RabbitMQ 741楼

<bean id="helloword" class="HelloWord"><property name="name" value="张三"></property></bean>

以上bean的配置的解读:class="HellloWord"是通过反射获取了一个同样的类的实例.而id="helloword",则是创建了一个bean,这个bean的名称是"helloword",被IOC容器管理的对象都称为bean.之后<property name="name" value="张三"></property>就是相当于setName()方法去设置属性的值

其实以上的代码可以对应了我们常规的用new去创建一个对象是一个原理的.比如:HelloWord helloword = new HelloWord();

new HelloWord();获得一个实例化对象,以上的代码是通过反射去获得的.再把这个实例化对象赋给要新建的那个对象.以上是新建一个bean,其实也是在新建一个对象而已,id则是这个新建的对象的名称.所以原理都是一样的.

从以上的解释中,是不是。

getBean(Class<T> type)要求类型唯一,就是指只能一个类型一个对象。原因是他是根据反射实例化的对象去找新建的对象的。所以出现多个新建对象他不知道是哪一个

比如:HelloWord helloword1 = new HelloWord();

           HelloWord helloword2 = new HelloWord();

通过new HelloWord()的有两个对象,去找会出现helloword1 和helloword2两个。

 而getBean(String name,Class<T> type)

相当于 通过new HelloWord()去找名为helloword1 或者helloword2的做到一对一

是不是这样理解的(我这是把bean标签里面的抽出来类比的讲了一下思路)

JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 742楼
Python 全系列/第十一阶段:重量级Web框架-Django/Redis的入门与应用(拓展) 746楼

1练习中问题一

老师金字塔是怎么做出的呢,我的代码是在哪一处有了问题的呢


代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>for循环联系打印</title>
</head>
<body>
<script>
   /* /!*打印5行5列的矩形
    * * * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *
    *!/
    for (var j=1;j<5;j++){  //外层循环控制行
        for (var i=1;i<5;i++){  //内层循环控制列
            document.write("*")
        }
        document.write("<br/>");
    }*/

    /*打印5行5列的矩形
    *
    * *
    * * *
    * * * *
    * * * * *

   for (var j=1;j<5;j++){  //外层循环控制行
       for (var i=1;i<=j;i++){  //内层循环控制列
           document.write("*")
       }
       document.write("<br/>");
   }*/
   /*打印5行5列的矩形
        *
       * *
      * * *
     * * * *
    * * * * *
    */
   var alt="";
    for (var i=1;i<=5;i++){  //控制行
        var blank=""
        for (var j=1;j<=5-i;j++){  //空白三角形
            blank+=""
        }
        var stars="";
        for (var m=1;m<=2*i-1;m++){
            stars+="*";
        }
        alt+=blank+stars+"\n";
    }
    console.log(alt);

</script>
</body>
</html>

运行图.

image.png


作业图image.png


WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 748楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 749楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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