python:treewidght使用、字典使用、鼠标右键功能、Fernet加密、中文保存.bin、文件权限修改、QMessageBox reply使用
主窗口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'caiya_2.ui' # # Created by: PyQt5 UI code generator 5.14.2 # # 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 .verticalLayout = QtWidgets.QVBoxLayout( self .centralwidget) self .verticalLayout.setObjectName( "verticalLayout" ) self .label = QtWidgets.QLabel( self .centralwidget) font = QtGui.QFont() font.setFamily( "华文楷体" ) font.setPointSize( 16 ) self .label.setFont(font) self .label.setAlignment(QtCore.Qt.AlignCenter) self .label.setObjectName( "label" ) self .verticalLayout.addWidget( self .label) self .line = QtWidgets.QFrame( self .centralwidget) self .line.setFrameShape(QtWidgets.QFrame.HLine) self .line.setFrameShadow(QtWidgets.QFrame.Sunken) self .line.setObjectName( "line" ) self .verticalLayout.addWidget( self .line) self .horizontalLayout = QtWidgets.QHBoxLayout() self .horizontalLayout.setObjectName( "horizontalLayout" ) self .pushButton = QtWidgets.QPushButton( self .centralwidget) font = QtGui.QFont() font.setPointSize( 12 ) self .pushButton.setFont(font) self .pushButton.setText("") self .pushButton.setObjectName( "pushButton" ) self .horizontalLayout.addWidget( self .pushButton) self .lineEdit = QtWidgets.QLineEdit( self .centralwidget) font = QtGui.QFont() font.setFamily( "Times New Roman" ) font.setPointSize( 12 ) self .lineEdit.setFont(font) self .lineEdit.setObjectName( "lineEdit" ) self .horizontalLayout.addWidget( self .lineEdit) self .verticalLayout.addLayout( self .horizontalLayout) self .verticalLayout_2 = QtWidgets.QVBoxLayout() self .verticalLayout_2.setObjectName( "verticalLayout_2" ) self .treeWidget = QtWidgets.QTreeWidget( self .centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch( 0 ) sizePolicy.setVerticalStretch( 0 ) sizePolicy.setHeightForWidth( self .treeWidget.sizePolicy().hasHeightForWidth()) self .treeWidget.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily( "Times New Roman" ) font.setPointSize( 12 ) self .treeWidget.setFont(font) self .treeWidget.setObjectName( "treeWidget" ) self .treeWidget.headerItem().setText( 0 , "1" ) self .verticalLayout_2.addWidget( self .treeWidget) self .horizontalLayout_3 = QtWidgets.QHBoxLayout() self .horizontalLayout_3.setObjectName( "horizontalLayout_3" ) self .textBrowser = QtWidgets.QTextBrowser( self .centralwidget) font = QtGui.QFont() font.setFamily( "Times New Roman" ) font.setPointSize( 12 ) self .textBrowser.setFont(font) self .textBrowser.setObjectName( "textBrowser" ) self .horizontalLayout_3.addWidget( self .textBrowser) self .verticalLayout_3 = QtWidgets.QVBoxLayout() self .verticalLayout_3.setObjectName( "verticalLayout_3" ) self .textBrowser_2 = QtWidgets.QTextBrowser( self .centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch( 0 ) sizePolicy.setVerticalStretch( 0 ) sizePolicy.setHeightForWidth( self .textBrowser_2.sizePolicy().hasHeightForWidth()) self .textBrowser_2.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily( "宋体" ) font.setPointSize( 12 ) self .textBrowser_2.setFont(font) self .textBrowser_2.setObjectName( "textBrowser_2" ) self .verticalLayout_3.addWidget( self .textBrowser_2) self .horizontalLayout_2 = QtWidgets.QHBoxLayout() self .horizontalLayout_2.setObjectName( "horizontalLayout_2" ) self .label_3 = QtWidgets.QLabel( self .centralwidget) font = QtGui.QFont() font.setFamily( "楷体" ) font.setPointSize( 12 ) self .label_3.setFont(font) self .label_3.setObjectName( "label_3" ) self .horizontalLayout_2.addWidget( self .label_3) self .lineEdit_2 = QtWidgets.QLineEdit( self .centralwidget) font = QtGui.QFont() font.setFamily( "宋体" ) font.setPointSize( 12 ) self .lineEdit_2.setFont(font) self .lineEdit_2.setObjectName( "lineEdit_2" ) self .horizontalLayout_2.addWidget( self .lineEdit_2) self .verticalLayout_3.addLayout( self .horizontalLayout_2) self .horizontalLayout_3.addLayout( self .verticalLayout_3) self .verticalLayout_2.addLayout( self .horizontalLayout_3) self .verticalLayout.addLayout( self .verticalLayout_2) MainWindow.setCentralWidget( self .centralwidget) self .statusbar = QtWidgets.QStatusBar(MainWindow) self .statusbar.setObjectName( "statusbar" ) MainWindow.setStatusBar( self .statusbar) self .retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi( self , MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate( "MainWindow" , "mainwindow" )) self .label.setText(_translate( "MainWindow" , "电子记录" )) self .label_3.setText(_translate( "MainWindow" , "目录" )) |
副窗口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'caiya_3.ui' # # Created by: PyQt5 UI code generator 5.14.2 # # 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 .verticalLayout = QtWidgets.QVBoxLayout( self .centralwidget) self .verticalLayout.setObjectName( "verticalLayout" ) self .label_3 = QtWidgets.QLabel( self .centralwidget) self .label_3.setAlignment(QtCore.Qt.AlignCenter) self .label_3.setObjectName( "label_3" ) self .verticalLayout.addWidget( self .label_3) self .horizontalLayout_2 = QtWidgets.QHBoxLayout() self .horizontalLayout_2.setObjectName( "horizontalLayout_2" ) self .label_2 = QtWidgets.QLabel( self .centralwidget) self .label_2.setObjectName( "label_2" ) self .horizontalLayout_2.addWidget( self .label_2) self .lineEdit = QtWidgets.QLineEdit( self .centralwidget) self .lineEdit.setObjectName( "lineEdit" ) self .horizontalLayout_2.addWidget( self .lineEdit) self .verticalLayout.addLayout( self .horizontalLayout_2) self .horizontalLayout_3 = QtWidgets.QHBoxLayout() self .horizontalLayout_3.setObjectName( "horizontalLayout_3" ) self .label_4 = QtWidgets.QLabel( self .centralwidget) self .label_4.setObjectName( "label_4" ) self .horizontalLayout_3.addWidget( self .label_4) self .lineEdit_2 = QtWidgets.QLineEdit( self .centralwidget) self .lineEdit_2.setObjectName( "lineEdit_2" ) self .horizontalLayout_3.addWidget( self .lineEdit_2) self .label_5 = QtWidgets.QLabel( self .centralwidget) self .label_5.setObjectName( "label_5" ) self .horizontalLayout_3.addWidget( self .label_5) self .lineEdit_3 = QtWidgets.QLineEdit( self .centralwidget) self .lineEdit_3.setObjectName( "lineEdit_3" ) self .horizontalLayout_3.addWidget( self .lineEdit_3) self .verticalLayout.addLayout( self .horizontalLayout_3) self .horizontalLayout_4 = QtWidgets.QHBoxLayout() self .horizontalLayout_4.setObjectName( "horizontalLayout_4" ) self .label_6 = QtWidgets.QLabel( self .centralwidget) self .label_6.setObjectName( "label_6" ) self .horizontalLayout_4.addWidget( self .label_6) self .lineEdit_4 = QtWidgets.QLineEdit( self .centralwidget) self .lineEdit_4.setObjectName( "lineEdit_4" ) self .horizontalLayout_4.addWidget( self .lineEdit_4) self .verticalLayout.addLayout( self .horizontalLayout_4) self .line = QtWidgets.QFrame( self .centralwidget) self .line.setFrameShape(QtWidgets.QFrame.HLine) self .line.setFrameShadow(QtWidgets.QFrame.Sunken) self .line.setObjectName( "line" ) self .verticalLayout.addWidget( self .line) self .horizontalLayout_5 = QtWidgets.QHBoxLayout() self .horizontalLayout_5.setObjectName( "horizontalLayout_5" ) self .label_7 = QtWidgets.QLabel( self .centralwidget) self .label_7.setObjectName( "label_7" ) self .horizontalLayout_5.addWidget( self .label_7) self .lineEdit_5 = QtWidgets.QLineEdit( self .centralwidget) self .lineEdit_5.setAlignment(QtCore.Qt.AlignCenter) self .lineEdit_5.setObjectName( "lineEdit_5" ) self .horizontalLayout_5.addWidget( self .lineEdit_5) self .verticalLayout.addLayout( self .horizontalLayout_5) self .label = QtWidgets.QLabel( self .centralwidget) self .label.setObjectName( "label" ) self .verticalLayout.addWidget( self .label) self .textEdit = QtWidgets.QTextEdit( self .centralwidget) self .textEdit.setObjectName( "textEdit" ) self .verticalLayout.addWidget( self .textEdit) self .horizontalLayout = QtWidgets.QHBoxLayout() self .horizontalLayout.setObjectName( "horizontalLayout" ) self .pushButton = QtWidgets.QPushButton( self .centralwidget) self .pushButton.setObjectName( "pushButton" ) self .horizontalLayout.addWidget( self .pushButton) spacerItem = QtWidgets.QSpacerItem( 40 , 20 , QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self .horizontalLayout.addItem(spacerItem) self .pushButton_2 = QtWidgets.QPushButton( self .centralwidget) self .pushButton_2.setObjectName( "pushButton_2" ) self .horizontalLayout.addWidget( self .pushButton_2) self .verticalLayout.addLayout( self .horizontalLayout) MainWindow.setCentralWidget( self .centralwidget) self .statusbar = QtWidgets.QStatusBar(MainWindow) self .statusbar.setObjectName( "statusbar" ) MainWindow.setStatusBar( self .statusbar) self .retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi( self , MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate( "MainWindow" , "信息输入" )) self .label_3.setText(_translate( "MainWindow" , "基本信息" )) self .label_2.setText(_translate( "MainWindow" , "标题:" )) self .label_4.setText(_translate( "MainWindow" , "姓名:" )) self .label_5.setText(_translate( "MainWindow" , "班级:" )) self .label_6.setText(_translate( "MainWindow" , "链接:" )) self .label_7.setText(_translate( "MainWindow" , "分类:" )) self .label.setText(_translate( "MainWindow" , "备注消息:" )) self .pushButton.setText(_translate( "MainWindow" , "确定" )) self .pushButton_2.setText(_translate( "MainWindow" , "取消" )) |
main代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | import sys from cryptography.fernet import Fernet import os from stat import S_IROTH,S_IWRITE import struct import random import pickle import qtawesome from PyQt5 import QtWidgets from PyQt5.QtWidgets import QApplication, QMessageBox, QTreeWidgetItem, QMenu from PyQt5.QtCore import Qt import caiya_2 import caiya_3 key_list = [b 'm8jj9QWq2zmebJyvF8d_WFmUGflkfNbP-QVms7gJ4Vc=' , b 'EcuWvJ2aWUrAJ3Wdt0LmnfJy6ES3YzWvR4MP7ilwCLY=' , b '00Aj75upYXW1YB9lGm-vWeTulAQ3FWarxH6IumzBAf8=' , b 'K14MYHjJvQljdIlVPU0TjWYaI_8MmsekwlvU9hE9K4A=' , b '1jWrIDnrW9IKNW_Q0NKE_jssc-Wad73bfgHVT8GmBpU=' , b 'OUqmLUQhti5lEdKL6YPYWx94Xmo-QiZBX1t9gK0OvTU=' , b '5RIhRzs5KS7e3DyKzc_4SN3pmxIlzltLmP8lK1CzP5E=' , b 'pgAYIISedDl1rK0ag96-B08bOrB3iaoxyU9kOPpKRnw=' , b 'aldczwkDaBwvdtgCgIRU5gGZPtOK25VUAb7cLKc0XT0=' , b '94q4-JHp_xaKiSjq1Tto8NAqllCCDYMlkjiCwS1Y9rs=' ] class my_mainwindow( object ): def __init__( self ): app = QApplication(sys.argv) app.setWindowIcon(qtawesome.icon( 'fa.shield' , color = 'blue' )) ######################### self .myMainWindow = MainWindow() self .myui = caiya_2.Ui_MainWindow() self .myui.setupUi( self .myMainWindow) self .Secondary_window = QtWidgets.QMainWindow() self .mini = caiya_3.Ui_MainWindow() self .mini.setupUi( self .Secondary_window) self .Secondary_window.setFixedSize( 600 , 400 ) # 禁止缩放 self .Secondary_window.setWindowFlags(Qt.WindowMinimizeButtonHint) # 仅保存缩小按钮 # self.mini.lineEdit_5.setEnabled(False) self .myui.pushButton.setIcon(qtawesome.icon( 'fa.search' , color = 'blue' )) #******************************************************************************** #获取数据 self .dictforcontent = self .strread(key_list) if self .dictforcontent = = {}: self .dictforcontent = { '标题' : [ '新建条目' ], '姓名' : [ '输入名字~' ], '班级' : [ 'xxx' ], '链接' : [ 'xxx' ], '备注' : [ 'xxx' ], 'ID' : [ 'xxx' ]} listforsort = set ( self .dictforcontent[ "ID" ]) #end #treewidght创建 self .myui.treeWidget.setHeaderLabels([ "目录" , "简述" ]) for name in listforsort: root = QTreeWidgetItem( self .myui.treeWidget) # 创建节点 root.setIcon( 0 , qtawesome.icon( 'fa.folder' , color = 'green' )) root.setText( 0 , name) # 设置顶级节点文本 for i in range ( len ( self .dictforcontent[ "ID" ])): # 遍历字典的ID值 value = self .dictforcontent[ "ID" ][i] if value = = name: child = QTreeWidgetItem(root) # 创建子节点 child.setIcon( 0 , qtawesome.icon( 'fa.hand-o-right' , color = 'black' )) child.setText( 0 , self .dictforcontent[ "标题" ][i]) # 设置第一列的值 child.setText( 1 , self .dictforcontent[ "姓名" ][i]) # 设置第二列的值 self .myui.treeWidget.setAlternatingRowColors( True ) self .myui.treeWidget.addTopLevelItem(root) # 将创建的树节点添加到树控件中 #单机绑定事件 self .myui.treeWidget.clicked.connect( self .gettext) #右键功能开启 self .myui.treeWidget.setContextMenuPolicy(Qt.CustomContextMenu) # 打开右键菜单的策略 self .myui.treeWidget.customContextMenuRequested.connect( self .do_custom_context_menu_request) # 绑定事件 #相关变量初始化 self .Right_click = True # treewidght的右键功能使能 self .row_num = - 1 # 节点的行号 self .parent_num = - 1 # 存放行的父节点的行号 self .childcnt = 0 #保存字典选中的inx self .lastID = '' #保存原始ID #按键绑定打开 self .click_pushbutton( self .myMainWindow) #******************************************************************************** self .myMainWindow.show() ##################### sys.exit(app.exec_()) def click_pushbutton( self ,myMainWindow): self .myui.pushButton.clicked.connect( lambda : self .find(myMainWindow)) self .mini.pushButton.clicked.connect( self .modify) self .mini.pushButton_2.clicked.connect( self .miniclose) def find( self ,myMainWindow): findstr = self .myui.lineEdit.text() if len (findstr) = = 0 : self .myui.textBrowser_2.clear() self .myui.textBrowser_2.setText( "空气搜不到哦" ) else : cnt_match = 0 self .myui.textBrowser_2.clear() for i in range ( len ( self .dictforcontent[ "标题" ])): result = findstr in self .dictforcontent[ "标题" ][i] if result = = True : cnt_match = cnt_match + 1 self .myui.textBrowser_2.append( "**匹配姓名%d**" % (cnt_match)) self .myui.textBrowser_2.append( "姓名匹配:%s" % ( self .dictforcontent[ "标题" ][i])) self .myui.textBrowser_2.append( "目录ID:%s" % ( self .dictforcontent[ "ID" ][i])) if cnt_match = = 0 : self .myui.textBrowser_2.append( "未发现匹配内容" ) def gettext( self ): account = self .myui.treeWidget.currentItem().text( 0 ) self .myui.lineEdit_2.setText(account) for i in range ( len ( self .dictforcontent[ "标题" ])): # 遍历字典的标题值 value = self .dictforcontent[ "标题" ][i] if value = = account: self .myui.textBrowser.clear() self .myui.textBrowser.append( "标题:%s" % self .dictforcontent[ "标题" ][i]) self .myui.textBrowser.append( "姓名:%s" % self .dictforcontent[ "姓名" ][i]) self .myui.textBrowser.append( "班级:%s" % self .dictforcontent[ "班级" ][i]) self .myui.textBrowser.append( "链接:%s" % self .dictforcontent[ "链接" ][i]) self .myui.textBrowser.append( "备注:%s" % self .dictforcontent[ "备注" ][i]) def do_custom_context_menu_request( self ,pos): row_num = - 1 # 当前节点的行号 parent_num = - 1 # 存放当前行的父节点的行号 if self .myui.treeWidget.selectionModel().selection().indexes() ! = [] and self .Right_click: for i in self .myui.treeWidget.selectionModel().selection().indexes(): row_num = i.row() parent_num = i.parent().row() if parent_num = = - 1 : menu = QMenu() item1 = menu.addAction(u "删除这一分类" ) item2 = menu.addAction(u "增加一个分类" ) item3 = menu.addAction(u "修改这一分类" ) item4 = menu.addAction(u "增加一条记录" ) action = menu.exec_( self .myui.treeWidget.mapToGlobal(pos)) if action = = item1: IDmodifyflag = 0 # 判断当前目录下是否有值 for i in self .dictforcontent[ "ID" ]: if i = = self .myui.treeWidget.currentItem().text( 0 ): QMessageBox.information( self .myMainWindow, "提醒你一下" , "这个目录下面有内容,先删掉内容" , QMessageBox.Yes) IDmodifyflag = 1 break if IDmodifyflag = = 0 : reply = QMessageBox.question( self .myMainWindow, "问你一下" , "你确定要删除吗?" , QMessageBox.Yes | QMessageBox.No) if reply = = QtWidgets.QMessageBox.Yes: delitem = self .myui.treeWidget.currentItem() rootIndex = self .myui.treeWidget.indexOfTopLevelItem(delitem) self .myui.treeWidget.takeTopLevelItem(rootIndex) self .myui.textBrowser_2.clear() self .myui.textBrowser_2.setText( "目录删除成功" ) elif action = = item2: reply = QMessageBox.question( self .myMainWindow, "问你一下" , "确定要增加一条并且目录已经在右下角更新好了吗?" , QMessageBox.Yes | QMessageBox.No) if reply = = QtWidgets.QMessageBox.Yes: IDmodifyflag = 0 # 判断顶层节点是否重复 # 获取最顶级节点的总数 rootCount = self .myui.treeWidget.topLevelItemCount() for i in range (rootCount): # 取得顶层项的item索引地址 rootItem = self .myui.treeWidget.topLevelItem(i) rootKey = rootItem.text( 0 ) if rootKey = = str ( self .myui.lineEdit_2.text()): QMessageBox.information( self .myMainWindow, "提醒你一下" , "你重复了" , QMessageBox.Yes) IDmodifyflag = 1 break if IDmodifyflag = = 0 : root = QTreeWidgetItem( self .myui.treeWidget) # 创建节点 root.setIcon( 0 , qtawesome.icon( 'fa.folder' , color = 'green' )) root.setText( 0 , self .myui.lineEdit_2.text()) # 设置顶级节点文本 self .myui.textBrowser_2.clear() self .myui.textBrowser_2.setText( "目录增加成功" ) elif action = = item3: reply = QMessageBox.question( self .myMainWindow, "问你一下" , "确定要修改吗?" , QMessageBox.Yes | QMessageBox.No) if reply = = QtWidgets.QMessageBox.Yes: IDmodifyflag = 0 # 判断ID是否重复 for i in self .dictforcontent[ "ID" ]: if i = = self .myui.lineEdit_2.text() and self .myui.lineEdit_2.text()! = self .myui.treeWidget.currentItem().text( 0 ): QMessageBox.information( self .myMainWindow, "提醒你一下" , "修改内容已存在" , QMessageBox.Yes) IDmodifyflag = 1 break if IDmodifyflag = = 0 : for i in range ( len ( self .dictforcontent[ "ID" ])): if self .dictforcontent[ "ID" ][i] = = self .myui.treeWidget.currentItem().text( 0 ): self .dictforcontent[ "ID" ][i] = self .myui.lineEdit_2.text() self .myui.treeWidget.currentItem().setText( 0 , self .myui.lineEdit_2.text()) # 更新数据库 self .strwrite(key_list, self .dictforcontent) self .myui.textBrowser_2.clear() self .myui.textBrowser_2.setText( "目录修改成功" ) elif action = = item4: flagforcontinue = 0 for i in self .dictforcontent[ "标题" ]: if i = = "新增条目" : QMessageBox.information( self .myMainWindow, "提醒你一下" , "已经有新增的" ,QMessageBox.Yes) flagforcontinue = 1 break if flagforcontinue = = 0 : reply = QMessageBox.question( self .myMainWindow, "问你一下" , "确定要增加一条吗?" , QMessageBox.Yes | QMessageBox.No) if reply = = QtWidgets.QMessageBox.Yes: self .dictforcontent[ "标题" ].append( "新增条目" ) self .dictforcontent[ "姓名" ].append( "记得要修改它哦" ) self .dictforcontent[ "班级" ].append( "xxx" ) self .dictforcontent[ "链接" ].append( "xxx" ) self .dictforcontent[ "备注" ].append( "xxx" ) self .dictforcontent[ "ID" ].append( self .myui.treeWidget.currentItem().text( 0 )) # 根据传入的节点 Item 和节点名称,增加子节点 item = self .myui.treeWidget.currentItem() childItem = QtWidgets.QTreeWidgetItem(item) childItem.setIcon( 0 , qtawesome.icon( 'fa.hand-o-right' , color = 'black' )) childItem.setText( 0 , self .dictforcontent[ "标题" ][ - 1 ]) # 设置第一列的值 childItem.setText( 1 , self .dictforcontent[ "姓名" ][ - 1 ]) # 设置第二列的值 self .myui.textBrowser_2.clear() self .myui.textBrowser_2.setText( "条目增加成功" ) else : return else : menu = QMenu() item1 = menu.addAction(u "删除这条记录" ) item2 = menu.addAction(u "修改一条记录" ) action = menu.exec_( self .myui.treeWidget.mapToGlobal(pos)) if action = = item1: reply = QMessageBox.question( self .myMainWindow, "问你一下哦" , "你确定要删除吗?" , QMessageBox.Yes | QMessageBox.No) if reply = = QtWidgets.QMessageBox.Yes: delitem = self .myui.treeWidget.currentItem() delitem.parent().removeChild(delitem) self .find_dict_inx(parent_num, row_num) #删除字典对应内容 del self .dictforcontent[ "标题" ][ self .childcnt] del self .dictforcontent[ "姓名" ][ self .childcnt] del self .dictforcontent[ "班级" ][ self .childcnt] del self .dictforcontent[ "链接" ][ self .childcnt] del self .dictforcontent[ "备注" ][ self .childcnt] del self .dictforcontent[ "ID" ][ self .childcnt] # 更新数据库 self .strwrite(key_list, self .dictforcontent) self .myui.textBrowser_2.clear() self .myui.textBrowser_2.setText( "数据删除成功" ) elif action = = item2: reply = QMessageBox.question( self .myMainWindow, "问你一下" , "确定要修改吗?" , QMessageBox.Yes | QMessageBox.No) if reply = = QtWidgets.QMessageBox.Yes: self .Right_click = False self .parent_num = parent_num self .row_num = row_num self .find_dict_inx(parent_num,row_num) #显示源数据 self .mini.lineEdit.setText( self .dictforcontent[ "标题" ][ self .childcnt]) self .mini.lineEdit_2.setText( self .dictforcontent[ "姓名" ][ self .childcnt]) self .mini.lineEdit_3.setText( self .dictforcontent[ "班级" ][ self .childcnt]) self .mini.lineEdit_4.setText( self .dictforcontent[ "链接" ][ self .childcnt]) self .mini.textEdit.setText( self .dictforcontent[ "备注" ][ self .childcnt]) self .lastID = self .myui.treeWidget.topLevelItem( self .parent_num).text( 0 ) self .mini.lineEdit_5.setText( self .lastID) self .Secondary_window.show() else : return def find_dict_inx( self ,userparent_num,userrow_num): self .childcnt = 0 toptreename = self .myui.treeWidget.topLevelItem(userparent_num).text( 0 ) for i in range ( len ( self .dictforcontent[ "ID" ])): if self .dictforcontent[ "ID" ][i] = = toptreename: self .childcnt = self .childcnt + 1 if self .childcnt = = (userrow_num + 1 ): self .childcnt = i break def modify( self ): modifyflag = 0 IDchanged = 0 IDexist = 0 #判断新建标题是否重复 for i in range ( len ( self .dictforcontent[ "标题" ])): if self .dictforcontent[ "标题" ][i] = = str ( self .mini.lineEdit.text()) and i! = self .childcnt: QMessageBox.information( self .myMainWindow, "提醒你一下" , "你的标题重复" , QMessageBox.Yes) modifyflag = 1 self .Right_click = True break #判断ID是否当前已存在 for i in self .dictforcontent[ "ID" ]: if i = = str ( self .mini.lineEdit_5.text()): IDexist = 1 break if IDexist = = 0 : QMessageBox.information( self .myMainWindow, "提醒你一下" , "你的分类不存在" , QMessageBox.Yes) modifyflag = 1 #ID不存在 self .Right_click = True elif str ( self .mini.lineEdit_5.text()) = = self .lastID: IDchanged = 1 #ID未改变 #不重复则更新数据 if modifyflag = = 0 : #更新数据 self .dictforcontent[ "标题" ][ self .childcnt] = str ( self .mini.lineEdit.text()) self .dictforcontent[ "姓名" ][ self .childcnt] = str ( self .mini.lineEdit_2.text()) self .dictforcontent[ "班级" ][ self .childcnt] = str ( self .mini.lineEdit_3.text()) self .dictforcontent[ "链接" ][ self .childcnt] = str ( self .mini.lineEdit_4.text()) self .dictforcontent[ "备注" ][ self .childcnt] = str ( self .mini.textEdit.toPlainText()) if IDchanged = = 1 : #更新treewidght self .myui.treeWidget.topLevelItem( self .parent_num).child( self .row_num).setText( 0 , self .dictforcontent[ "标题" ][ self .childcnt]) self .myui.treeWidget.topLevelItem( self .parent_num).child( self .row_num).setText( 1 , self .dictforcontent[ "姓名" ][ self .childcnt]) elif IDchanged = = 0 : self .dictforcontent[ "ID" ][ self .childcnt] = str ( self .mini.lineEdit_5.text()) #删除当前tree delitem_parent = self .myui.treeWidget.topLevelItem( self .parent_num) delitem = delitem_parent.child( self .row_num) delitem.parent().removeChild(delitem) #别的tree去新建 rootCount = self .myui.treeWidget.topLevelItemCount() print (rootCount) for i in range (rootCount): # 取得顶层项的item索引地址 rootItem = self .myui.treeWidget.topLevelItem(i) rootKey = rootItem.text( 0 ) print (rootKey) print ( self .mini.lineEdit_5.text()) if rootKey = = str ( self .mini.lineEdit_5.text()): print ( "进来了" ) # 根据传入的节点 Item 和节点名称,增加子节点 childItem = QtWidgets.QTreeWidgetItem(rootItem) childItem.setIcon( 0 , qtawesome.icon( 'fa.hand-o-right' , color = 'black' )) childItem.setText( 0 , self .dictforcontent[ "标题" ][ self .childcnt]) # 设置第一列的值 childItem.setText( 1 , self .dictforcontent[ "姓名" ][ self .childcnt]) # 设置第二列的值 break # 更新数据库 self .strwrite(key_list, self .dictforcontent) self .myui.textBrowser_2.clear() self .myui.textBrowser_2.setText( "数据修改成功" ) # 关闭窗口 self .Right_click = True self .Secondary_window.close() def miniclose( self ): self .Right_click = True self .Secondary_window.close() def strwrite( self ,keys, write_dict): isfile = os.path.exists( './Data/Database.bin' ) if not isfile: self .myui.textBrowser.clear() self .myui.textBrowser.append( "完蛋,数据库不翼而飞!" ) else : key_inx = random.randint( 0 , 9 ) # 创建加密工具 cipher = Fernet(keys[key_inx]) # 字典转为字符串(bytes数据类型) serialized = pickle.dumps(write_dict) # 进行加密 encrypted_text = cipher.encrypt(serialized) # bytes数据类型转为str数据类型 encrypted_str = encrypted_text.decode( "unicode_escape" ) # 字符串转为asii码 encrypted_strtoasii = [] for i in encrypted_str: encrypted_strtoasii.append( ord (i)) # ord为将字符串转为asii码数 encrypted_strtoasii.append(key_inx) # 文件权限修改为读写 os.chmod( "./Data/Database.bin" , S_IWRITE) # 写入内容 with open ( "./Data/Database.bin" , "wb" ) as fp: # a表示在文档末尾添加内容,w则表示清除原来的内容从新写 b表示以二进制形式打开 for x in encrypted_strtoasii: a = struct.pack( "B" , x) # 将整数转换为二进制字符串 fp.write(a) # 文件权限修改为只读 os.chmod( "./Data/Database.bin" , S_IROTH) def strread( self ,keys): ispath = os.path.exists( './Data' ) if not ispath: os.makedirs(r "./Data" ) isfile = os.path.exists( './Data/Database.bin' ) if not isfile: with open ( './Data/Database.bin' , 'ab' ) as fp: pass # 读取文件数据 with open ( "./Data/Database.bin" , "rb" ) as fp: text = fp.read() content = struct.unpack( "B" * len (text), text) # 解包 strlist = [] decrypted_str = "" deserialized = {} if len (content) = = 0 : keyinx = - 1 else : keyinx = int (content[ - 1 ]) if len (content) = = 0 : self .myui.textBrowser_2.clear() self .myui.textBrowser_2.append( "数据库空空如也" ) elif len (content) % 2 = = 0 or keyinx > 9 or keyinx < 0 : self .myui.textBrowser_2.clear() self .myui.textBrowser_2.append( "数据库好像有问题,删掉从新来过" ) else : self .myui.textBrowser_2.clear() self .myui.textBrowser_2.setText( "请尽情查看^_^" ) try : for i in range ( int ( len (content) - 1 )): a = content[i] strlist.append( chr (a)) # chr表示将asii码转为对应字符 except Exception as exc: returnlist = str (exc) self .myui.textBrowser_2.clear() self .myui.textBrowser_2.setText(returnlist) else : self .myui.textBrowser_2.setText( "数据库没问题" ) # 字符列表转为字符串 decrypted_str = decrypted_str.join(strlist) # 字符串转为bytes数据类型 decrypted_str_text = decrypted_str.encode( "unicode_escape" ) # 进行解密 cipher = Fernet(keys[keyinx]) decrypted_text = cipher.decrypt(decrypted_str_text) # 字符串转为字典 deserialized = pickle.loads(decrypted_text) return deserialized class MainWindow(QtWidgets.QMainWindow): """对QMainWindow类重写,实现一些功能""" def closeEvent( self , event): """重写该方法主要是解决打开子窗口时,如果关闭了主窗口但子窗口仍显示的问题, 使用sys.exit(0) 时就会只要关闭了主窗口,所有关联的子窗口也会全部关闭""" sys.exit( 0 ) if __name__ = = '__main__' : my_mainwindow() |
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具