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ù),并封裝為一個列表
cursor.execute('select * from user')
result = cursor.fetchall()
#print(result)
users=[]
for record in result:
#將數(shù)據(jù)庫中的一條記錄,封裝為一個user對象
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()
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):
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])
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ù),并封裝為一個列表
cursor.execute('select * from user')
result = cursor.fetchall()
#print(result)
users=[]
for record in result:
#將數(shù)據(jù)庫中的一條記錄,封裝為一個user對象
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()