一、整理筆記
1.數(shù)據(jù)庫(kù)的連接和數(shù)據(jù)的增刪改查
import mysql.connector
# 連接到指定的數(shù)據(jù)庫(kù)
db = mysql.connector.connect(host='localhost',user='root',password='123456',database='JY40',port = '3307')
cursor.execute('select * from user')
# #獲取查詢結(jié)果集
result = cursor.fetchall()
# #每條結(jié)果封裝為一個(gè)元組
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.設(shè)置窗口的按鈕
def initUI(self):
#創(chuàng)建提示
self.setToolTip('這是一個(gè)<b>QWidget</b>組件')
#創(chuàng)建按鈕
btn = QPushButton('按鈕',self)
#移動(dòng)按鈕的位置
btn.move(100,100)
self.setGeometry(300,300,300,200)
self.setWindowTitle('提示信息')
self.show()
4.給按鈕綁定功能
def initUI(self):
#創(chuàng)建按鈕
btn = QPushButton('按鈕',self)
#移動(dòng)按鈕位置
btn.move(100,100)
#給按鈕綁定功能 當(dāng)點(diǎn)擊按鈕時(shí)才去執(zhí)行,
btn.clicked.connect(self.f)
# btn.clicked.connect(QCoreApplication.instance().quit)#點(diǎn)擊按鈕退出
self.setGeometry(300,300,300,200)
self.setWindowTitle('給按鈕綁定功能')
self.show()
def f(self):
print('測(cè)試按鈕功能!')
#重寫QWidget的closeEvent()f昂發(fā),
#該方法在關(guān)閉事件發(fā)生時(shí)會(huì)自動(dòng)調(diào)用
def closeEvent(self,event):
reply = QMessageBox.question(self,'Message','你準(zhǔn)備退出嗎?',QMessageBox.Yes | QMessageBox.No)
#根據(jù)用戶的選擇進(jìn)行處理
if reply ==QMessageBox.Yes:
event.accept()#接受事件
else:
event.ignore()#忽略事件
框式布局:
def initUI(self):
hbox = QHBoxLayout()
hbox.addWidget(ok)
hbox.addStretch(1)
hbox.addWidget(cancel)
#創(chuàng)建垂直布局
vbox = QVBoxLayout()
vbox.addStretch(2)
vbox.addLayout(hbox)
# self.setLayout(hbox)
self.setLayout(vbox)
self.setGeometry(300,300,300,200)
self.setWindowTitle('框式布局')
self.show()
|