PyQt项目实战
Pyqt5实现todolist工作待办的增、改、删、查功能,连接mysql存储数据(待办事项)。
目录
目录
1.界面设计
2.功能实现
3.界面各Button点击事件和槽函数的链接
4.mysql数据库操作工具类
5.槽函数实现数据库操作
1.界面设计
支持新增待办事项、修改事项的内容,可选择事项的状态(完成/待完成),可设置用户的发薪日期,实时显示当前时间。效果图:
界面设计代码:
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(431, 679)
Style = CommonHelper.readQss("TODO.qss")
Form.setStyleSheet(Style)
self.frame = QFrame(Form)
self.frame.setObjectName(u"frame")
self.frame.setFrameShape(QFrame.StyledPanel)
self.frame.setFrameShadow(QFrame.Raised)
self.Time_Label = QLabel(self.frame)
self.Time_Label.setObjectName(u"Time_Label")
self.label_2 = QLabel(self.frame)
self.label_2.setObjectName(u"label_2")
self.verticalLayoutWidget_3 = QWidget(self.frame)
self.verticalLayoutWidget_3.setObjectName(u"verticalLayoutWidget_3")
self.verticalLayout_3 = QVBoxLayout(self.verticalLayoutWidget_3)
self.verticalLayout_3.setObjectName(u"verticalLayout_3")
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.label_9 = QLabel(self.verticalLayoutWidget_3)
self.label_9.setObjectName(u"label_9")
self.verticalLayout_3.addWidget(self.label_9)
self.label_10 = QLabel(self.verticalLayoutWidget_3)
self.label_10.setObjectName(u"label_10")
self.verticalLayout_3.addWidget(self.label_10)
self.label_11 = QLabel(self.verticalLayoutWidget_3)
self.label_11.setObjectName(u"label_11")
self.verticalLayout_3.addWidget(self.label_11)
self.verticalLayoutWidget = QWidget(self.frame)
self.verticalLayoutWidget.setObjectName(u"verticalLayoutWidget")
self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.label_3 = QLabel(self.verticalLayoutWidget)
self.label_3.setObjectName(u"label_3")
self.verticalLayout.addWidget(self.label_3)
self.label_4 = QLabel(self.verticalLayoutWidget)
self.label_4.setObjectName(u"label_4")
self.verticalLayout.addWidget(self.label_4)
self.label_5 = QLabel(self.verticalLayoutWidget)
self.label_5.setObjectName(u"label_5")
self.verticalLayout.addWidget(self.label_5)
self.img = QLabel(self.frame)
self.img.setObjectName(u"img")
self.horizontalFrame = QFrame(self.frame)
self.horizontalFrame.setObjectName(u"horizontalFrame")
self.horizontalLayout = QHBoxLayout(self.horizontalFrame)
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.pushButton_4 = QPushButton(self.horizontalFrame)
self.pushButton_4.setObjectName(u"pushButton_4")
icon = QIcon()
icon.addFile(u":/img/imgs/tips.png", QSize(), QIcon.Normal, QIcon.Off)
self.pushButton_4.setIcon(icon)
self.horizontalLayout.addWidget(self.pushButton_4)
self.pushButton_3 = QPushButton(self.horizontalFrame)
self.pushButton_3.setObjectName(u"pushButton_3")
icon1 = QIcon()
icon1.addFile(u":/img/imgs/help.png", QSize(), QIcon.Normal, QIcon.Off)
self.pushButton_3.setIcon(icon1)
self.horizontalLayout.addWidget(self.pushButton_3)
self.pushButton_2 = QPushButton(self.horizontalFrame)
self.pushButton_2.setObjectName(u"pushButton_2")
icon2 = QIcon()
icon2.addFile(u":/img/imgs/close.png", QSize(), QIcon.Normal, QIcon.Off)
self.pushButton_2.setIcon(icon2)
self.horizontalLayout.addWidget(self.pushButton_2)
self.listWidget = QListWidget(self.frame)
self.listWidget.setObjectName(u"listWidget")
self.pushButton = QPushButton(self.frame)
self.pushButton.setObjectName(u"pushButton")
icon = QIcon()
icon.addFile(u":/img/imgs/add.png", QSize(), QIcon.Normal, QIcon.Off)
self.pushButton.setIcon(icon)
self.frame.setGeometry(QRect(0, 0, 431, 481))
self.Time_Label.setGeometry(QRect(10, 10, 231, 31))
self.label_2.setGeometry(QRect(20, 40, 391, 41))
self.verticalLayoutWidget_3.setGeometry(QRect(20, 90, 91, 80))
self.verticalLayoutWidget.setGeometry(QRect(110, 90, 91, 80))
self.img.setGeometry(QRect(320, 40, 100, 100))
self.horizontalFrame.setGeometry(QRect(300, 0, 131, 42))
self.listWidget.setGeometry(QRect(10, 190, 411, 192))
self.pushButton.setGeometry(QRect(370, 437, 51, 41))
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
self.Time_Label.setText(QCoreApplication.translate("Form", u"\u73b0\u5728\u65f6\u95f4:", None))
self.label_2.setText(QCoreApplication.translate("Form", u"12:15:30", None))
self.label_9.setText(QCoreApplication.translate("Form", u"\u79bb\u53d1\u85aa", None))
self.label_10.setText(QCoreApplication.translate("Form", u"10", None))
self.label_11.setText(QCoreApplication.translate("Form", u"\u5929", None))
self.label_3.setText(QCoreApplication.translate("Form", u"\u79bb\u5468\u516d", None))
self.label_4.setText(QCoreApplication.translate("Form", u"10", None))
self.label_5.setText(QCoreApplication.translate("Form", u"\u5929", None))
self.img.setText("")
__sortingEnabled = self.listWidget.isSortingEnabled()
self.listWidget.setSortingEnabled(False)
self.listWidget.setSortingEnabled(__sortingEnabled)
self.pushButton.clicked.connect(self.addItemToListWidget)
2.功能实现
(1)实时显示当前时间
# 定义定时器
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_time_and_labels)
self.timer.start(1000) # 每秒更新一次
(2)新增待办事项
@Slot()
def addItemToListWidget(self):
# 创建一个可选中的项
item = QListWidgetItem()
item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
item.setCheckState(Qt.Unchecked)
item.setText("") # 默认文本
item.setFlags(
Qt.ItemIsSelectable | Qt.ItemIsEditable | Qt.ItemIsDragEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled); # 可编辑文本
# 添加到listWidget中
self.listWidget.addItem(item)
# 使得项可编辑
self.listWidget.editItem(item)
(3)修改事项的内容
self.listWidget.editItem(item)
标签:setObjectName,Form,self,verticalLayoutWidget,PyQt,label,Pyqt5,TODO,pushButton From: https://blog.csdn.net/qq_56959393/article/details/142579799