一、工作內(nèi)容:
9:00-20:00
1.'''ORM Object Relationship Mapping 對(duì)象關(guān)系映射'''
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表中所有數(shù)據(jù),并封裝為一個(gè)列表
cursor.execute('select * from user')
result = cursor.fetchall()
#print(result)
users=[]
for record in result:
#將數(shù)據(jù)庫中的一條記錄,封裝為一個(gè)user對(duì)象
user=User(record[0],record[1],record[2])
users.append(user)
print(record)
return users
def find_by_id(self,id):
# '''根據(jù)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('測(cè)試窗口')
window.show()
sys.exit(app.exec_())
3.設(shè)置窗口
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtGui import QIcon
import sys
class Example(QWidget):
def __init__(self):
#調(diào)用父類的構(gòu)造方法(必須調(diào)用,否則等同于沒有繼承)
super().__init__()
self.initUI()
def initUI(self):
#設(shè)置窗口大小和位置
self.setGeometry(0,0,10,200)
#設(shè)置窗口標(biāo)題
self.setWindowTitle('圖標(biāo)')
#修改圖標(biāo)
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.創(chuàng)建提示和按鈕
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):
#設(shè)置提示的字體 像素pixel(px)
QToolTip.setFont(QFont('微軟雅黑',10))
#創(chuàng)建提示
self.setToolTip('這是一個(gè)<b>QWidget</b>組件')
#創(chuàng)建按鈕
btn=QPushButton('按鈕',self)
#創(chuàng)建提示
btn.setToolTip('這是一個(gè)<i>按鈕</i>組件')
#移動(dòng)按鈕的位置
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):
#創(chuàng)建按鈕
btn = QPushButton('退出',self)
#移動(dòng)按鈕位置
btn.move(100,100)
#給按鈕綁定功能
#btn.clicked.connect(self.f) # ()里面必須給一個(gè)函數(shù),當(dāng)按鈕被按下后,執(zhí)行函數(shù),只寫函數(shù)名不加括號(hào)
btn.clicked.connect(QCoreApplication.instance().quit)
self.setGeometry(300,300,300,200)
self.setWindowTitle('給按鈕綁定功能呢')
self.show()
def f(self):
print("測(cè)試按鈕功能!")
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()方法,該方法在關(guān)閉事件發(fā)生時(shí)會(huì)自動(dòng)調(diào)用
def closeEvent(self,event):
#print('closeEvent被調(diào)用了')
reply = QMessageBox.question(self,'message','你真的準(zhǔn)備退出嗎?',QMessageBox.Yes|QMessageBox.No)
#根據(jù)用戶的選擇進(jìn)行處理
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):
#創(chuàng)建2個(gè)按鈕
ok = QPushButton('OK')
cancel = QPushButton('Cancel')
#創(chuàng)建水平布局
hbox =QHBoxLayout()
hbox.addStretch(2)
hbox.addWidget(ok)
hbox.addStretch(1)
hbox.addWidget(cancel)
hbox.addStretch(2)
#創(chuàng)建垂直布局
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):
#創(chuàng)建一個(gè)表格布局
grid = QGridLayout()
self.setLayout(grid)
#創(chuàng)建所有按鈕標(biāo)簽
labels = ['<——','CE','C','Close',
'7','8','9','/',
'4','5','6','*',
'1','2','3','-',
'0','.','=','+']
#創(chuàng)建按鈕的位置參數(shù)
positions = [(x,y) for x in range(5) for y in range(4)]
#print(positions)
#創(chuàng)建按鈕并添加到表格中
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('計(jì)算器')
self.show()
if __name__ =='__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
21:00-23:00
完成調(diào)用函數(shù)對(duì)數(shù)據(jù)庫進(jìn)行增刪改查操作作業(yè)
二、遇到的問題
調(diào)用函數(shù)對(duì)數(shù)據(jù)庫進(jìn)行增刪改查
三、處理方式(查看類筆記)
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表中所有數(shù)據(jù),并封裝為一個(gè)列表
cursor.execute('select * from user')
result = cursor.fetchall()
#print(result)
users=[]
for record in result:
#將數(shù)據(jù)庫中的一條記錄,封裝為一個(gè)user對(duì)象
user=User(record[0],record[1],record[2])
users.append(user)
print(record)
return users
def find_by_id(self,id):
# '''根據(jù)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)
|