주소록 GUI 프로그램
MySQL DB 연동 GUI 개발
-
MySQL
-
Qt Designer
-
addressBookApp.py
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import pymysql
class qtApp(QMainWindow):
conn=None
curIdx = 0
def __init__(self):
super().__init__()
uic.loadUi('./PyQt_Practice/addressBook.ui',self)
self.setWindowIcon(QIcon('./PyQt_practice/address-book.png'))
self.initDB()
self.btnNew.clicked.connect(self.btnNewClicked)
self.btnSave.clicked.connect(self.btnSaveClicked)
self.tblAddress.doubleClicked.connect(self.tblAddressDoubleClicked)
self.btnDel.clicked.connect(self.btnDelClicked)
def btnNewClicked(self):
self.txtName.setText('')
self.txtPhone.setText('')
self.txtEmail.setText('')
self.txtAddress.setText('')
self.txtName.setFocus()
self.curIdx =0
def btnSaveClicked(self):
fullName=self.txtName.text()
phoneNum = self.txtPhone.text()
email=self.txtEmail.text()
address=self.txtAddress.text()
if fullName=='' or phoneNum=='':
QMessageBox.warning(self,'주의','이름과 휴대폰 번호를 입력하세요')
return
else:
self.conn=pymysql.connect(host='localhost',user='root',password='12345',
db='miniproject',charset='utf8')
if self.curIdx ==0:
query = '''
INSERT INTO addressbook (FullName,PhoneNum,Email,Address)
VALUES(%s,%s,%s,%s)
'''
else:
query = ''' UPDATE addressbook
SET FullName = %s
, PhoneNum = %s
, Email = %s
, Address = %s
WHERE idx = %s '''
cur=self.conn.cursor()
if self.curIdx==0:
cur.execute(query,(fullName,phoneNum,email,address))
else:
cur.execute(query,(fullName,phoneNum,email,address,self.curIdx))
self.conn.commit()
self.conn.close()
if self.curIdx==0:
QMessageBox.about(self,'성공','INSERT Completed')
else:
QMessageBox.about(self,'성공','Update Completed')
self.initDB()
self.btnNewClicked()
def tblAddressDoubleClicked(self):
rowIndex=self.tblAddress.currentRow()
self.txtName.setText(self.tblAddress.item(rowIndex,1).text())
self.txtPhone.setText(self.tblAddress.item(rowIndex,2).text())
self.txtEmail.setText(self.tblAddress.item(rowIndex,3).text())
self.txtAddress.setText(self.tblAddress.item(rowIndex,4).text())
self.curIdx = int(self.tblAddress.item(rowIndex,0).text())
def btnDelClicked(self):
if self.curIdx ==0:
QMessageBox.warning(self,'경고','삭제할 데이터를 선택하세요')
else:
reply=QMessageBox.question(self,'확인','정말로 삭제하시겠습니까?',QMessageBox.Yes|QMessageBox.No,
QMessageBox.Yes)
if reply==QMessageBox.No:
return
self.conn=pymysql.connect(host='localhost',user='root',password='12345',
db='miniproject',charset='utf8')
query='DELETE FROM addressbook WHERE idx=%s'
cur=self.conn.cursor()
cur.execute(query,(self.curIdx))
self.conn.commit()
self.conn.close()
QMessageBox.about(self,'성공','Delete Completed')
self.initDB()
self.btnNewClicked()
def initDB(self):
self.conn=pymysql.connect(host='localhost',user='root',password='12345',
db='miniproject',charset='utf8')
cur=self.conn.cursor()
query='''
SELECT idx
, FullName
, PhoneNum
, Email
, Address
FROM addressbook
'''
cur.execute(query)
rows=cur.fetchall()
self.makeTable(rows)
self.conn.close()
def makeTable(self,rows):
self.tblAddress.setColumnCount(5)
self.tblAddress.setRowCount(len(rows))
self.tblAddress.setSelectionMode(QAbstractItemView.SingleSelection)
self.tblAddress.setHorizontalHeaderLabels(['번호','이름','휴대폰','이메일','주소'])
self.tblAddress.setColumnWidth(0,0)
self.tblAddress.setColumnWidth(1,70)
self.tblAddress.setColumnWidth(2,105)
self.tblAddress.setColumnWidth(3,175)
self.tblAddress.setColumnWidth(4,200)
self.tblAddress.setEditTriggers(QAbstractItemView.NoEditTriggers)
for i,row in enumerate(rows):
idx=row[0]
fullName=row[1]
phoneNum=row[2]
email=row[3]
address=row[4]
self.tblAddress.setItem(i,0,QTableWidgetItem(str(idx)))
self.tblAddress.setItem(i,1,QTableWidgetItem(fullName))
self.tblAddress.setItem(i,2,QTableWidgetItem(phoneNum))
self.tblAddress.setItem(i,3,QTableWidgetItem(email))
self.tblAddress.setItem(i,4,QTableWidgetItem(address))
self.stbCurrent.showMessage(f'전체 주소록 : {len(rows)}개')
if __name__=='__main__':
app=QApplication(sys.argv)
ex=qtApp()
ex.show()
sys.exit(app.exec_())
주소록 저장
주소록 변경
주소록 삭제