会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132382个问题
Python 全系列/第一阶段:Python入门/编程基本概念 3377楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 3378楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Canvas多个小球随机运动.html</title>
    <style>
        canvas{
            border: 1px solid #000;
        }
    </style>
</head>
<body>
<canvas width="400px" height="300px">您的浏览器不支持canvas,请升级浏览器!</canvas>
</body>
<script>
    /*var modul = (function () {
        function getElement(eleName) {
            return document.querySelector(eleName);
        }
        return {
            getElement:getElement
        };
    })();
    var canvas = modul.getElement("canvas");
    var ctx = canvas.getContext("2d");
    console.log(canvas);*/
    var canvas = document.querySelector("canvas");
    var ctx = canvas.getContext("2d");

    function randomColor() {
        var r = parseInt(Math.random() * 256);
        var g = parseInt(Math.random() * 256);
        var b = parseInt(Math.random() * 256);
        var CircleColor = "rgb(" + r + "," + g + "," + b + ")";
        return CircleColor;
    }
    function Circle() {
        this.r = Math.floor(Math.random() * 20 + 11);
        this.x = Math.floor(Math.random() * (400 - this.r));
        this.y = Math.floor(Math.random() * (300 - this.r));
        this.speedX = Math.floor(Math.random() * 8);
        this.speedY = Math.floor(Math.random() * 6);
        this.color = randomColor();
    }
    Circle.prototype.circleSpeed = function () {
        this.x += this.speedX;
        if (this.x <= this.r){
            this.speedX = -this.speedX;
        }else if (this.x >= (400 - this.r)){
            this.speedX = -this.speedX;
        }
        this.y += this.speedY;
        if (this.y <= this.r){
            this.speedY = -this.speedY;
        }else if (this.y >= (300 - this.r)){
            this.speedY = -this.speedY;
        }
    };
    Circle.prototype.drawCircle = function () {
        ctx.beginPath();
        ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
        ctx.fillStyle = this.color;
        ctx.fill();
        ctx.closePath();
    };
    var arr = [];
    for (var i = 0; i < 4; i++){
        arr[i] = new Circle();
    }
    var timer = setInterval(function () {
        ctx.clearRect(0,0,canvas.width,canvas.height);
        for (var i = 0; i < arr.length; i++){
            arr[i] = circleSpeed();
            arr[i] = drawCircle();
        }
    },18);
</script>
</html>

哪里有问题?

WEB前端全系列/第九阶段:HTML5新特性模块/(旧)H5新特性 3383楼

#DBUtil的代码

import pymysql
class DBUtil:
    config={
        'host':'localhost',
        'port':3306,
        'user':'root',
        'passwd':'ftzlxy',
        'db':'music_project',
        'charset':'utf8'
    }


    def __init__(self) -> None:
        #获取连接,获取游标
        self.con=pymysql.connect(**DBUtil.config)
        self.cursor=self.con.cursor()

    def close(self)->None:
        #关闭游标,关闭连接
        if self.cursor:
            self.cursor.close()
        if self.con:
            self.con.close()


        #封装dml
    def execute_dml(self,sql,*args):            #不封装时用*args
        #可以执行dml语句用于数据的增删改
        #执行sql
        try:
            id=self.con.insert_id()
            self.cursor.execute(sql,args)
            #提交事务
            self.con.commit()
            return id
        except Exception as e:
            print(e)
            if self.con:
                self.con.rollback()
        finally:
            self.close()

        #封装dql
    def query_one(self,sql,*args):
        #获取一条数据
        try:
            self.cursor.execute(sql,args)
            rs=self.cursor.fetchone()
            return rs
        except Exception as e:
            print(e)
        finally:
            self.close()
    def query_many(self,sql,*args):
        #获取多条数据
        pass
    def query_all(self,sql,*args):
        #获取所有数据
        pass
   

if __name__=='__main__':
   
    db=DBUtil()
    sql='''
     select * from t_user;
    '''
    print(db.query_one(sql))


#MyService的代码

from dbutil import DBUtil

class MyService:

    def __init__(self) -> None:
        self.user=None
    def login(self,uname,passwd):
        sql='select * from t_user where uname=%s and passwd=%s'
        user=DBUtil().query_one(sql,uname,passwd)                 #!!!!!引用类时记得加()如DBUtil().query_one(sql) 而不是DBUtil.query_one(sql)
        if user:
            self.user=user
            return True
        else:
            return False

    def add_music(self,files):
        for f in files:
            #取音乐名字
            start=f.rfind("/")+1
            end=f.rfind(".mp3")
            music_name=f[start:end]
            print(music_name)
            #将音乐保存到t_music表
            sql="insert into t_music(music_name,path) values(%s,%s)"
            mid=DBUtil().execute_dml(sql,music_name,f)        #f是路径-->path
            print(mid)
            #用户选择的音乐保存到t_list
            sql="insert into t_list(mid,uid) values(%s,%s)"
            DBUtil().execute_dml(sql,mid,self.user[0])         #[0]:第一个字段

if __name__=="__main__":
    pass
    #db=MyService()
    #print(db.login())


老师,我因为外键的原因报错了,通过输出mid我发现mid一直是0而不是insert进t_music的id,检查代码后也没发现错误,请问该如何解决呢

image.png

Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 3386楼

# encoding=utf-8
from tkinter import Tk, Frame, Menu, Text, INSERT, END
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.filename = None
        self.contextMenu = None
        self.pack()
        self.createWidget()

    def createWidget(self):
        menubar = Menu(root)

        menuFile = Menu(menubar)
        menuEdit = Menu(menubar)
        menuHelp = Menu(menubar)

        menubar.add_cascade(label='file(F)', menu=menuFile)
        menubar.add_cascade(label='edit(E)', menu=menuEdit)
        menubar.add_cascade(label='help(H)', menu=menuHelp)

        menuFile.add_command(label='new', accelerator='ctrl+n', command=self.newfile)
        menuFile.add_command(label='open', accelerator='ctrl+o', command=self.openfile)
        menuFile.add_command(label='save', accelerator='ctrl+s', command=self.savefile)
        menuFile.add_separator()
        menuFile.add_command(label='exit', 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='background color', command=self.openAskColor)

        root.bind('<Button-3>', self.createContextMenu)

    def newfile(self):
        self.textpad.delete('1.0', 'end')
        self.filename = asksaveasfilename(title='save as', initialfile='unnamed.txt',
                                          filetypes=[('text document', '*txt')],
                                          defaultextension='.txt')
        self.savefile()

    def openfile(self):
        self.textpad.delete('1.0', 'end')
        with open(askopenfile(title='open file')) 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='choose colors')
        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+200+300')
    root.title('My notebook')
    app = Application(master=root)
    root.mainloop()

老师您好,在这一个视频中我也出现了好多问题,根据其他同学的提问,老师们的解答,和百度知乎上解决了一部分,到了这一步没找到解决方法:就是打包时一直提示:

(venv) C:\Users\刘琪\PycharmProjects\GUI>pyinstaller -F mytest.py

failed to create process.

麻烦老师解答,谢谢


Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 3387楼
Python 全系列/第一阶段:Python入门/函数和内存分析 3388楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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