会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132453个问题
JAVA 全系列/第十四阶段:全文检索服务/ElasticSearch 16531楼
Python 全系列/第一阶段:Python入门/编程基本概念 16533楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 16535楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 16537楼

<script>
        //登录按钮的点击事件
        //点击时ajax请求去后台,并等待后台反馈数据
        $(".loginBtn").click(function() {
            //发起ajax请求
            var userName = $('.username').val();
            var passWord = $('.password').val();
            //判空操作
            if (userName.trim().length == 0 || passWord.trim().length == 0) {
                alert('用户名或密码不能为空,请检查输入');
                return;
            }
            $.ajax({
                    type: 'post',
                    url: 'PHP/loginBtn.php',
                    dataType: 'json',
                    data: {
                        uname: userName,
                        upass: passWord
                    },
                    success: function(res) {
                        // console.log(res);
                        switch (res.info) {
                            case 0:
                                {
                                    alert('登陆成功');
                                }
                                break;
                            case 1:
                                {
                                    alert('登录失败,用户名或密码错误');
                                }
                                break;
                            case 2:
                                {
                                    alert('登录失败,网络连接失败');
                                }
                                break;
                            case 3:
                                {
                                    alert('登录失败,该用户名不存在');
                                }
                                break;
                            default:
                                {
                                    alert('未知错误');
                                }
                        }
                    }
                })
                //发生请求完毕后,初始化输入框
            $('.username').val('');
            $('.password').val('');
        })
    </script>
<?php
    //获取用户从前端发来的数据
    $username=$_POST['uname'];
    $password=$_POST['upass'];
    $success=array('mag'=>'ok');
    //连接数据库
    //0 成功  1 失败  2 数据库连接失败 3 数据库为空 
    $con=mysqli_connect('localhost','root','','lanmei');
    if($con){
        mysqli_query($con,'set names utf8');
        mysqli_query($con,'set character_set_client utf8');
        mysqli_query($con,'set character_set_results utf8');
        $sql='select * from user';
        $result=$con->query($sql);
        //读取数据库中的用户信息
        if($result->num_rows>0){
            $str=[];
            for($i=0;$row=$result->fetch_assoc();$i++){
                $str[i]=$row;
            }
            //判断发来的用户名和密码,是否在数据库中有对因信息
            $flag=false;//标识符,默认登录失败
            for($j=0;j<count($str);$j++){
                if($str[$j]['username']==$username){
                    if($str[$j]['password']==$password){
                        $success['info']=0;
                        $flag=true;
                        break;
                    }
                }
            }
            //当循环结束后,判断$flag的值
            if(!$flag){
                $success['info']=1;
            }

        }else{
            $success['info']=3;
        }
    
    }else{
        $success['info']=2;
    }
    echo json_decode($success);
?>

登录功能实现不了,也没有报错

image.png

WEB前端全系列/第六阶段:音乐社区高级项目模块/移动端:基于jQuery使用Ajax和BootStrap 16539楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask视图基础和URL 16540楼

"""
开发一个简单的记事本。
包含: 新建,保存,修改文本文件,退出
包含; 各种快捷键的处理
"""
from tkinter.filedialog import *
from tkinter.colorchooser import *


class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.textpad = None
        self.filename = None
        self.contextMenu = None
        self.pack()
        self.createWidget()

    def createWidget(self):
        menubar = Menu(root)
        root["menu"] = 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_command(label="退出", accelerator="ctrl+q", command=self.exit)

        root.bind("<Control-n>", lambda event: self.newfile())
        root.bind("<Control-0>", lambda event: self.openfile())
        root.bind("<Control-s>", lambda event: self.savefile())
        root.bind("<Control-h>", 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")
        print(self.filename)
        self.savefile()

    def openfile(self):
        with askopenfile(title="打开文件") as f:
            self.textpad.insert(INSERT, f.read())
            self.filename = f.name
            print(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("400x100+200+100")
    root.title("记事本")
    app = Application(master=root)
    root.mainloop()

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Users\Inspiron\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__

    return self.func(*args)

  File "D:/WorkSpace/py_case/pythonProject/FirstGUI/twenty-two.py", line 63, in savefile

    with open(self.filename, "w") as f:

TypeError: expected str, bytes or os.PathLike object, not NoneType

调用Savefile方法总是提示这个错误,newfile方法里调用也失败,老师求解答!

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 16541楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 16542楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/虚拟环境 16543楼
Python 全系列/ 第十四阶段:自动化操作办公软件、邮件、定时任务等/自动化操作办公软件、邮件、定时任务等 16545楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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