我的账户
啄木鸟学院

专注软件测试菁英教育

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

python学习第8天_刘国平_2021.01.26

[复制链接]
果丹卷学员认证 发表于 2021-1-26 22:03:11 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
一、工作内容:
9:00-20:00
1.'''ORM Object Relationship Mapping 对象关系映射'''
import mysql.connector
db = mysql.connector.connect(host='localhost',port=3307,user='root',password='123456',database='jy40')
cursor= db.cursor()

class User:
    def __init__(self,id,name,pwd):
        self.id=id
        self.name=name
        self.pwd=pwd

class UserDB:
    def find_all(self):
        #查询user表中所有数据,并封装为一个列表   
        cursor.execute('select * from user')
        result = cursor.fetchall()
        #print(result)
        users=[]
        for record in result:
            #将数据库中的一条记录,封装为一个user对象
            user=User(record[0],record[1],record[2])
            users.append(user)
            print(record)
        return users

    def find_by_id(self,id):
    #     '''根据id查询指定用户信息'''
        cursor.execute('select * from user where id=%s',(id,))
        #user= User()
        u = cursor.fetchone()
        print(u)

    def insert(self,name,pwd):
        cursor.execute('insert into user (name,pwd) values(%s,%s)',(name,pwd))
        db.commit()
    # '''添加新用户'''

    def delete(self,id):
        cursor.execute('delete from user where id=%s',(id,))
        db.commit()   

    def update(self,pwd,id):
        cursor.execute('update user set pwd=%s where id=%s',(pwd,id))
        db.commit()

a=UserDB()
a.update('654321',1)
a.insert('fff','123456')
a.find_by_id(18)
a.find_all()
a.delete(16)

2.窗口
from PyQt5.QtWidgets import QApplication,QWidget
import sys

if __name__=='__main__':
    app = QApplication(sys.argv)
    window = QWidget()
    window.resize(300,200)
    window.move(300,300)
    window.setWindowTitle('测试窗口')
    window.show()

    sys.exit(app.exec_())

3.设置窗口
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtGui import QIcon
import sys

class Example(QWidget):
    def __init__(self):
        #调用父类的构造方法(必须调用,否则等同于没有继承)
        super().__init__()
        self.initUI()

    def initUI(self):
        #设置窗口大小和位置
        self.setGeometry(0,0,10,200)
        #设置窗口标题
        self.setWindowTitle('图标')
        #修改图标
        self.setWindowIcon(QIcon(r'D:\workspace\python\day08\f9.png'))
        #显示窗口
        self.show()

if __name__ =="__main__":
    app=QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

4.创建提示和按钮
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QToolTip
from PyQt5.QtGui import QFont
import sys
class Example(QWidget):


    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        #设置提示的字体 像素pixel(px)
        QToolTip.setFont(QFont('微软雅黑',10))
        #创建提示
        self.setToolTip('这是一个<b>QWidget</b>组件')


        #创建按钮
        btn=QPushButton('按钮',self)
        #创建提示
        btn.setToolTip('这是一个<i>按钮</i>组件')
        #移动按钮的位置
        btn.move(100,100)

        self.setGeometry(300,300,300,300)
        self.setWindowTitle('提示信息')
        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

5.为按钮增加功能
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton
from PyQt5.QtCore import QCoreApplication
import sys
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        #创建按钮
        btn = QPushButton('退出',self)
        #移动按钮位置
        btn.move(100,100)
        #给按钮绑定功能
        #btn.clicked.connect(self.f)      # ()里面必须给一个函数,当按钮被按下后,执行函数,只写函数名不加括号
        btn.clicked.connect(QCoreApplication.instance().quit)      

        self.setGeometry(300,300,300,200)
        self.setWindowTitle('给按钮绑定功能呢')
        self.show()
    def f(self):
        print("测试按钮功能!")
if __name__ =="__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

6.事件
from PyQt5.QtWidgets import QApplication,QWidget,QMessageBox
import sys

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.setGeometry(300,300,300,200)
        self.setWindowTitle('消息窗口演示')
        self.show()

    '''

    事件 event
    closeEvent

    '''
    #重写QWidget的closeEvent()方法,该方法在关闭事件发生时会自动调用
    def closeEvent(self,event):
        #print('closeEvent被调用了')
        reply = QMessageBox.question(self,'message','你真的准备退出吗?',QMessageBox.Yes|QMessageBox.No)
        #根据用户的选择进行处理
        if reply == QMessageBox.Yes:
            event.accept()    #接受事件
        else:
            event.ignore()    #忽略事件
if __name__ =='__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

7.框式布局
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QHBoxLayout,QVBoxLayout
import sys
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        #创建2个按钮
        ok = QPushButton('OK')
        cancel = QPushButton('Cancel')

        #创建水平布局
        hbox =QHBoxLayout()
        hbox.addStretch(2)
        hbox.addWidget(ok)
        hbox.addStretch(1)
        hbox.addWidget(cancel)
        hbox.addStretch(2)
        #创建垂直布局
        vbox = QVBoxLayout()
        vbox.addStretch(15)
        vbox.addLayout(hbox)
        vbox.addStretch(1)


        self.setLayout(vbox)
        self.setGeometry(300,300,300,200)
        self.setWindowTitle('框式布局')
        self.show()
if __name__ =='__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

8.表格布局
from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QGridLayout
import sys

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        #创建一个表格布局
        grid = QGridLayout()
        self.setLayout(grid)
        #创建所有按钮标签

        labels = ['<——','CE','C','Close',
                     '7','8','9','/',
                     '4','5','6','*',
                     '1','2','3','-',
                     '0','.','=','+']
        #创建按钮的位置参数
        positions = [(x,y) for x in range(5) for y in range(4)]
        #print(positions)
        #创建按钮并添加到表格中
        for label,position in zip(labels,positions):
            btn= QPushButton(label)
            grid.addWidget(btn,*position)
            #grid.addWidget(label,position[0],position[1])

        # backspace = QPushButton('<——')
        # grid.addWidget(backspace,1,1)
        # ce = QPushButton('CE')
        # grid.addWidget(ce,1,2)

        self.move(500,500)
        self.setWindowTitle('计算器')
        self.show()
if __name__ =='__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

21:00-23:00
完成调用函数对数据库进行增删改查操作作业
二、遇到的问题
调用函数对数据库进行增删改查
三、处理方式(查看类笔记)
import mysql.connector
db = mysql.connector.connect(host='localhost',port=3307,user='root',password='123456',database='jy40')
cursor= db.cursor()

class User:
    def __init__(self,id,name,pwd):
        self.id=id
        self.name=name
        self.pwd=pwd

class UserDB:
    def find_all(self):
        #查询user表中所有数据,并封装为一个列表   
        cursor.execute('select * from user')
        result = cursor.fetchall()
        #print(result)
        users=[]
        for record in result:
            #将数据库中的一条记录,封装为一个user对象
            user=User(record[0],record[1],record[2])
            users.append(user)
            print(record)
        return users

    def find_by_id(self,id):
    #     '''根据id查询指定用户信息'''
        cursor.execute('select * from user where id=%s',(id,))
        #user= User()
        u = cursor.fetchone()
        print(u)

    def insert(self,name,pwd):
        cursor.execute('insert into user (name,pwd) values(%s,%s)',(name,pwd))
        db.commit()
    # '''添加新用户'''

    def delete(self,id):
        cursor.execute('delete from user where id=%s',(id,))
        db.commit()   

    def update(self,pwd,id):
        cursor.execute('update user set pwd=%s where id=%s',(pwd,id))
        db.commit()

a=UserDB()
a.update('654321',1)
a.insert('fff','123456')
a.find_by_id(18)
a.find_all()
a.delete(16)

回复

使用道具 举报

关注0

粉丝0

帖子15

发布主题
大家都在学
课堂讨论
一周热帖排行最近7x24小时热帖
关注我们
专注软件测试菁英教育

客服电话:17792550360

客服时间:9:00-21:00

卓目鸟学苑 - 专注软件测试菁英教育!( 陕ICP备2025058934号-2 )

版权所有 © 西安菁英教育科技有限公司 2023-2026