Python+pyqt5+designer 实现信息录入系统
主程序代码编写
import os,time,datetime,sys,random,math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets,QtGui
from PyQt5.QtCore import *
from Ui_info5 import Ui_MainWindow
from functools import partial
#导入数据库
from PyQt5 import QtSql
from PyQt5.QtSql import QSqlQuery
class Mywindow(Ui_MainWindow,QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.tableWidget.setRowCount(10)
self.current_page = 1
# 设置槽函数
self.pushButton.clicked.connect(self.charu)
self.pushButton.clicked.connect(partial(self.showdb, 0))
# self.pushButton_2.clicked.connect(self.renewtb)
self.pushButton_3.clicked.connect(partial(self.showdb, 0)) #首页
self.pushButton_4.clicked.connect(self.prepage) #上一页
self.pushButton_5.clicked.connect(self.nextpage) #下一页
self.pushButton_6.clicked.connect(partial(self.tbnum, 1)) #尾页
self.pushButton_7.clicked.connect(self.gotopage) #跳转页数
#链接sqlite数据库
self.database = QtSql.QSqlDatabase.addDatabase('QSQLITE')
self.database.setDatabaseName('test.db') #如果没有该文件则自动创建
self.database.open()
# self.table()
self.showdb(0)
self.tbnum(0)
#下一页
def nextpage(self):
n = (self.current_page)*10
if self.current_page > (self.total_page - 1):
Msg = Message()
Msg.msg2('已经最后一页了')
return
self.showdb(n)
self.current_page = self.current_page + 1
self.label_8.setText(str(self.current_page))
#上一页
def prepage(self):
n = (self.current_page-2)*10
if self.current_page < 2:
Msg = Message()
Msg.msg2('已经是第一页了')
return
self.showdb(n)
if n==0:
self.current_page = 1
self.label_8.setText(str(self.current_page))
else:
self.current_page = self.current_page - 1
self.label_8.setText(str(self.current_page))
#跳转指定页数
def gotopage(self):
the_page = int(self.lineEdit_3.text()) #获取页数
the_n = (the_page-1)*10
if the_page <= 0 or the_page > self.total_page:
#如果输入的数值超出范围,提示错误
Msg = Message()
Msg.msg2('输入的数值超出了范围,请重新输入')
else:
self.showdb(the_n)
self.label_8.setText(self.lineEdit_3.text())
self.current_page = the_page
#表格获取总条目 页数
def tbnum(self,x):
#获取总条目
query = QSqlQuery()
if query.exec('select * from person ORDER BY ID DESC'):
id_index = query.record().indexOf('id')
db_list = []
while query.next():
id = query.value(id_index)
db_list.append((id))
# print('总条目:'+ str(len(db_list)))
self.total_page = math.ceil(len(db_list) / 10)
self.label_5.setText('共'+str(self.total_page)+'页')
if x == 1: #点击尾页显示的数据
n = int(self.total_page-1)*10
self.showdb(n)
self.label_8.setText(str(self.total_page))
self.current_page = self.total_page
#可自定义表格
def table(self):
self.tableWidget.setColumnCount(3)
self.tableWidget.setRowCount(2)
j = 0#第几行(从0开始)
i = 0#第几列(从0开始)
Value = "test"#内容
self.tableWidget.setItem(j, i, QTableWidgetItem(Value))#设置j行i列的内容为Value
self.tableWidget.setColumnWidth(j,80)#设置j列的宽度
self.tableWidget.setRowHeight(i,50)#设置i行的高度
self.tableWidget.verticalHeader().setVisible(False)#隐藏垂直表头
self.tableWidget.horizontalHeader().setVisible(False)#隐藏水平表头
#表格刷新数据
def renewtb(self):
self.showdb()
#数据展示在表格上
def showdb(self,n):
query = QSqlQuery()
if query.exec('select * from person ORDER BY ID DESC limit {},10'.format(str(n))):
id_index = query.record().indexOf('id')
name_index = query.record().indexOf('name')
age_index = query.record().indexOf('age')
sex_index = query.record().indexOf('sex')
nr_index = query.record().indexOf('nr')
db = []
while query.next():
id = query.value(id_index)
name = query.value(name_index)
age = query.value(age_index)
sex = query.value(sex_index)
nr = query.value(nr_index)
# print(id,name,age,sex,nr)
db.append((id,name,age,sex,nr))
# print(db)
self.tableWidget.clearContents()#清除所有数据--不包括标题头
if n == 0:
self.label_8.setText('1')
self.current_page = 1
else:
self.label_8.setText(str(self.current_page))
self.tbnum(0)
for i in range(len(db)): #i是行数
# print(db[i])
# print(db[i][0])
self.tableWidget.setItem(i, 0, QTableWidgetItem(str(db[i][0])))
self.tableWidget.setItem(i, 1, QTableWidgetItem(db[i][1]))
self.tableWidget.setItem(i, 2, QTableWidgetItem(db[i][2]))
self.tableWidget.setItem(i, 3, QTableWidgetItem(db[i][3]))
self.tableWidget.setItem(i, 4, QTableWidgetItem(db[i][4]))#设置j行i列的内容为Value
#查询数据
def chaxundb(self):
query = QSqlQuery()
if query.exec('select * from person'):
id_index = query.record().indexOf('id')
name_index = query.record().indexOf('name')
age_index = query.record().indexOf('age')
sex_index = query.record().indexOf('sex')
nr_index = query.record().indexOf('nr')
db = []
while query.next():
id = query.value(id_index)
name = query.value(name_index)
age = query.value(age_index)
sex = query.value(sex_index)
nr = query.value(nr_index)
# print(id,name,age,sex,nr)
db.append((id,name,age,sex,nr))
print(db)
#插入数据
def charu(self):
query = QSqlQuery()
the_name = self.lineEdit.text() #获取姓名
the_age = self.lineEdit_2.text() #获取年龄
the_input = self.textEdit.toPlainText() #获取内容框的值
if self.radioButton.isChecked()==True: #判断性别
the_sex = '男'
else:
the_sex = '女'
print(the_sex)
sql_code ='INSERT INTO person (name,age,sex,nr) VALUES ("{}","{}","{}","{}")'.format(the_name,the_age,the_sex,the_input)
if query.exec_(sql_code):
print('succeed insert data')
Msg = Message()
Msg.msg2('添加成功')
self.lineEdit.setText('')
self.lineEdit_2.setText('')
self.textEdit.setText('')
#删除数据
def deletedb(self):
query = QSqlQuery()
sql_code ='DELETE FROM person WHERE ID = 7'
if query.exec_(sql_code):
print('Delete data Success')
#更新数据
def update(self):
query = QSqlQuery()
sql_code ='UPDATE person SET name = "DUU",age="18",sex="女",nr="哈哈哈从成功了" WHERE ID = 6'
if query.exec_(sql_code):
print('succeed update data')
#提示信息
#这里是消息提示弹出框的主要代码
class Message(QtWidgets.QWidget):
def __init__(self):
super().__init__()
#消息提示框
def msg(self):
QMessageBox.information(self,"提示框","复制成功")
# QMessageBox.question(self,"消息框标题","这是一条问答。")
# QMessageBox.information(self,"消息框标题","这是一条消息。")
def msg2(self,word):
QMessageBox.warning(self,"提示框",word)
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = Mywindow()
MainWindow.show()
sys.exit(app.exec_())
UI界面程序 designer设计好界面生成Ui_info5.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'f:\pydemo\Gui_Pyqt5\info5.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(20, 20, 761, 521))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.lineEdit = QtWidgets.QLineEdit(self.tab)
self.lineEdit.setGeometry(QtCore.QRect(120, 50, 331, 41))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.tab)
self.label.setGeometry(QtCore.QRect(70, 60, 54, 12))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.tab)
self.label_2.setGeometry(QtCore.QRect(70, 150, 54, 12))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.tab)
self.lineEdit_2.setGeometry(QtCore.QRect(120, 140, 331, 41))
self.lineEdit_2.setObjectName("lineEdit_2")
self.radioButton = QtWidgets.QRadioButton(self.tab)
self.radioButton.setGeometry(QtCore.QRect(120, 220, 89, 16))
self.radioButton.setChecked(True)
self.radioButton.setObjectName("radioButton")
self.label_3 = QtWidgets.QLabel(self.tab)
self.label_3.setGeometry(QtCore.QRect(70, 220, 54, 12))
self.label_3.setObjectName("label_3")
self.radioButton_2 = QtWidgets.QRadioButton(self.tab)
self.radioButton_2.setGeometry(QtCore.QRect(210, 220, 89, 16))
self.radioButton_2.setObjectName("radioButton_2")
self.label_4 = QtWidgets.QLabel(self.tab)
self.label_4.setGeometry(QtCore.QRect(70, 290, 54, 12))
self.label_4.setObjectName("label_4")
self.textEdit = QtWidgets.QTextEdit(self.tab)
self.textEdit.setGeometry(QtCore.QRect(120, 280, 541, 121))
self.textEdit.setObjectName("textEdit")
self.pushButton = QtWidgets.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(120, 430, 141, 51))
self.pushButton.setObjectName("pushButton")
self.tabWidget.addTab(self.tab, "")
self.widget = QtWidgets.QWidget()
self.widget.setObjectName("widget")
self.tableWidget = QtWidgets.QTableWidget(self.widget)
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 761, 501))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(10)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(6, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(7, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(8, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(9, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(0, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 1, item)
self.pushButton_2 = QtWidgets.QPushButton(self.widget)
self.pushButton_2.setGeometry(QtCore.QRect(590, 40, 101, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.widget)
self.pushButton_3.setGeometry(QtCore.QRect(40, 370, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.widget)
self.pushButton_4.setGeometry(QtCore.QRect(130, 370, 75, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.widget)
self.pushButton_5.setGeometry(QtCore.QRect(240, 370, 75, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(self.widget)
self.pushButton_6.setGeometry(QtCore.QRect(330, 370, 75, 23))
self.pushButton_6.setObjectName("pushButton_6")
self.label_5 = QtWidgets.QLabel(self.widget)
self.label_5.setGeometry(QtCore.QRect(420, 372, 54, 20))
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.widget)
self.label_6.setGeometry(QtCore.QRect(470, 370, 51, 21))
self.label_6.setObjectName("label_6")
self.lineEdit_3 = QtWidgets.QLineEdit(self.widget)
self.lineEdit_3.setGeometry(QtCore.QRect(500, 370, 41, 20))
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_7 = QtWidgets.QLabel(self.widget)
self.label_7.setGeometry(QtCore.QRect(550, 372, 54, 20))
self.label_7.setObjectName("label_7")
self.pushButton_7 = QtWidgets.QPushButton(self.widget)
self.pushButton_7.setGeometry(QtCore.QRect(570, 370, 75, 23))
self.pushButton_7.setObjectName("pushButton_7")
self.label_8 = QtWidgets.QLabel(self.widget)
self.label_8.setGeometry(QtCore.QRect(220, 372, 16, 20))
self.label_8.setObjectName("label_8")
self.tabWidget.addTab(self.widget, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "信息录入系统"))
self.label.setText(_translate("MainWindow", "姓名"))
self.label_2.setText(_translate("MainWindow", "年龄"))
self.radioButton.setText(_translate("MainWindow", "男"))
self.label_3.setText(_translate("MainWindow", "性别"))
self.radioButton_2.setText(_translate("MainWindow", "女"))
self.label_4.setText(_translate("MainWindow", "内容"))
self.pushButton.setText(_translate("MainWindow", "提交"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "录入信息"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "1"))
item = self.tableWidget.verticalHeaderItem(1)
item.setText(_translate("MainWindow", "2"))
item = self.tableWidget.verticalHeaderItem(2)
item.setText(_translate("MainWindow", "3"))
item = self.tableWidget.verticalHeaderItem(3)
item.setText(_translate("MainWindow", "4"))
item = self.tableWidget.verticalHeaderItem(4)
item.setText(_translate("MainWindow", "5"))
item = self.tableWidget.verticalHeaderItem(5)
item.setText(_translate("MainWindow", "6"))
item = self.tableWidget.verticalHeaderItem(6)
item.setText(_translate("MainWindow", "7"))
item = self.tableWidget.verticalHeaderItem(7)
item.setText(_translate("MainWindow", "8"))
item = self.tableWidget.verticalHeaderItem(8)
item.setText(_translate("MainWindow", "9"))
item = self.tableWidget.verticalHeaderItem(9)
item.setText(_translate("MainWindow", "10"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "ID"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "姓名"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "年龄"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "性别"))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "内容"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
self.tableWidget.setSortingEnabled(__sortingEnabled)
self.pushButton_2.setText(_translate("MainWindow", "刷新数据"))
self.pushButton_3.setText(_translate("MainWindow", "首页"))
self.pushButton_4.setText(_translate("MainWindow", "<上一页"))
self.pushButton_5.setText(_translate("MainWindow", "下一页>"))
self.pushButton_6.setText(_translate("MainWindow", "尾页"))
self.label_5.setText(_translate("MainWindow", "共10页"))
self.label_6.setText(_translate("MainWindow", "跳到"))
self.label_7.setText(_translate("MainWindow", "页"))
self.pushButton_7.setText(_translate("MainWindow", "确定"))
self.label_8.setText(_translate("MainWindow", "4"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.widget), _translate("MainWindow", "信息列表"))
文件下载
注意:因为我的电脑是64位的,所以生成的文件也是64位的可执行程序,在32位的电脑上是无法运行的,32位的程序打包方法可以参考下面的文章
联系站长
友情链接
其他入口
QQ与微信加好友
粤ICP备17018681号 站点地图 www.iamdu.com 版权所有 服务商提供:阿里云 Designed by :DU
本站部分资源内容来源于网络,若侵犯您的权益,请联系删除!