菁英科技(卓目鸟学苑)- 专注软件测试菁英教育

标题: python_武浩杰_20210126 [打印本页]

作者: 海底月是天上月    时间: 2021-1-26 20:50
标题: python_武浩杰_20210126
一、整理笔记
1.数据库的连接和数据的增删改查
import mysql.connector
# 连接到指定的数据库
db = mysql.connector.connect(host='localhost',user='root',password='123456',database='JY40',port = '3307')
cursor.execute('select * from user')
# #获取查询结果集
result = cursor.fetchall()
# #每条结果封装为一个元组
for record in result:
    print(record)
cursor.execute('select * from user where id =%s',(1,))
result = cursor.fetchone()
print(result)
# 修改id = 3 的元素,pwd='abc'
cursor.execute('update user set pwd=%s where id=%s',('abc',3))
#删除id = 2 的元素
cursor.execute('delete from user where id=%s',(17,))
db.commit()
3.设置窗口的按钮
    def initUI(self):
        #创建提示
        self.setToolTip('这是一个<b>QWidget</b>组件')
        #创建按钮
        btn = QPushButton('按钮',self)
        #移动按钮的位置
        btn.move(100,100)
        self.setGeometry(300,300,300,200)
        self.setWindowTitle('提示信息')
        self.show()
4.给按钮绑定功能
    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('测试按钮功能!')
    #重写QWidget的closeEvent()f昂发,
    #该方法在关闭事件发生时会自动调用
    def closeEvent(self,event):
        reply = QMessageBox.question(self,'Message','你准备退出吗?',QMessageBox.Yes | QMessageBox.No)
    #根据用户的选择进行处理
        if reply ==QMessageBox.Yes:
            event.accept()#接受事件
        else:
            event.ignore()#忽略事件         
框式布局:
    def initUI(self):
        hbox = QHBoxLayout()
        hbox.addWidget(ok)
        hbox.addStretch(1)
        hbox.addWidget(cancel)
        #创建垂直布局
        vbox = QVBoxLayout()
        vbox.addStretch(2)
        vbox.addLayout(hbox)
        # self.setLayout(hbox)
        self.setLayout(vbox)
        self.setGeometry(300,300,300,200)
        self.setWindowTitle('框式布局')
        self.show()






欢迎光临 菁英科技(卓目鸟学苑)- 专注软件测试菁英教育 (http://www.zmnxy.com/) Powered by Discuz! X3.4