line-height:normal 默认行高 html按钮点击无效button.disabled=true

本文介绍如何使用HTML设置按钮为不可点击状态,通过设置button元素的disabled属性为true实现按钮禁用效果,避免用户误操作。

line-height:normal

默认行高,设置成默认行高这样当字号较大时不会遮挡其他行的

 

 

 

 html按钮button.disabled=true

是html按钮点击无效

我让你改了很多次,你始终没有改好,现在我给你一段代码,你完全按照这段代码的样式改<template> <div class="container"> <!-- 头部区域(左侧留空,右侧放置筛选框) --> <div class="header"> <div class="left-section"> <!-- 左侧可放置标题或其他内容 --> </div> <div class="right-section"> <el-select v-model="selectedType" placeholder="选择日志类型" clearable style="width: 200px;" > <el-option v-for="type in typeOptions" :key="type" :label="type" :value="type" /> </el-select> </div> </div> <!-- 日志表格 --> <el-table :data="pagedLogs" style="width: 100%" border stripe v-loading="loading" > <el-table-column prop="sn" label="设备SN" width="180" /> <el-table-column prop="type" label="类型" width="100" /> <el-table-column prop="type_label" label="类型标签" width="120" /> <el-table-column prop="label" label="标签" width="120" /> <el-table-column prop="message" label="消息" min-width="300" /> <el-table-column prop="create_time" label="时间" width="180"> <template #default="{ row }"> {{ formatTime(row.create_time) }} </template> </el-table-column> </el-table> <!-- 分页组件 --> <div class="pagination-center"> <el-pagination background layout="total, prev, pager, next" :total="filteredLogs.length" :page-size="pageSize" v-model:current-page="currentPage" /> </div> </div> </template> <script setup> import { ref, computed, onMounted, onBeforeUnmount } from 'vue' // 响应式数据 const logs = ref([]) const selectedType = ref('') const eventSource = ref(null) const loading = ref(false) // 分页相关状态 const currentPage = ref(1) const pageSize = ref(20) // 从日志中提取所有日志类型(去重) const typeOptions = computed(() => { const types = new Set() logs.value.forEach(log => { if (log.type) types.add(log.type) }) return Array.from(types) }) // 过滤后的日志:根据选择的日志类型 const filteredLogs = computed(() => { if (!selectedType.value) return logs.value return logs.value.filter(log => log.type === selectedType.value) }) // 分页后的日志 const pagedLogs = computed(() => { const start = (currentPage.value - 1) * pageSize.value const end = start + pageSize.value return filteredLogs.value.slice(start, end) }) // 格式化时间 const formatTime = (timestamp) => { if (!timestamp) return '' try { const date = new Date(timestamp) return `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}` } catch { return timestamp } } // 初始化SSE连接 const initSSE = () => { const sseUrl = 'http://127.0.0.1:8888/sse/logs' if (eventSource.value) { eventSource.value.close() } loading.value = true eventSource.value = new EventSource(sseUrl) eventSource.value.onmessage = (event) => { try { const newLogs = JSON.parse(event.data) // 直接替换整个日志数组 logs.value = newLogs loading.value = false } catch (error) { console.error('解析日志数据失败:', error) loading.value = false } } eventSource.value.onerror = (error) => { console.error('SSE连接错误:', error) loading.value = false // 错误时尝试重新连接 setTimeout(initSSE, 3000) } } onMounted(() => { initSSE() }) onBeforeUnmount(() => { if (eventSource.value) { eventSource.value.close() } }) </script> <style scoped> .container { padding: 20px; height: 100%; display: flex; flex-direction: column; } .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; } .left-section { display: flex; align-items: center; } .right-section { display: flex; align-items: center; } .el-table { flex: 1; overflow: hidden; } /* 分页居中样式 */ .pagination-center { display: flex; justify-content: center; /* 水平居中 */ margin-top: 15px; } /* 调整分页组件样式 */ .el-pagination { --el-pagination-bg-color: #fff; --el-pagination-button-disabled-bg-color: #fff; --el-pagination-button-bg-color: #fff; --el-pagination-hover-color: #409eff; --el-pagination-button-radius: 4px; } /* 分页按钮样式优化 */ .el-pagination .btn-prev, .el-pagination .btn-next, .el-pager li { min-width: 32px; height: 32px; line-height: 32px; margin: 0 4px; border-radius: 4px; } /* 当前页样式 */ .el-pager li.is-active { background-color: #409eff; color: #fff; font-weight: bold; } /* 表格边框优化 */ .el-table--border { border: 1px solid #ebeef5; border-radius: 4px; } /* 单元格内边距 */ .el-table td, .el-table th { padding: 8px 0; } </style>
08-16
<template> <!-- 表格容器,根据 dialogTable 的值动态添加 'dialog-table' 类名 --> <div :class="{'dialog-table': dialogTable}" class="table-box"> <!-- Element UI 表格组件 --> <!-- ref: 为表格组件添加引用,方便后续操作 --> <!-- border: 是否显示表格边框 --> <!-- data: 表格展示的数据 --> <!-- header-cell-style: 设置表格表头的背景颜色 --> <!-- height: 设置表格的--> <!-- highlight-current-row: 是否亮当前选中 --> <!-- row-class-name: 自定义的类名 --> <!-- row-style: 设置的样式,此处设置为 24px --> <!-- stripe: 是否显示斑马纹 --> <!-- style: 设置表格的宽度和边框颜色 --> <!-- @selection-change: 当表格的选中项发生变化时触发的事件 --> <div class="table-container"> <el-table ref="table" v-loading="loading" :border="border" :data="tableData" :header-cell-style="{ background: headerBgColor }" :height="tableHeight" :highlight-current-row="highlightCurrentRow" :row-class-name="rowClassName" :row-style="{ height: '24px' }" :stripe="stripe" :style="{ width: '100%', borderColor: tableBorderColor }" @selection-change="handleSelectionChange" @row-dblclick="handleRowClick" @row-click="handleRowClickSelect" > <!-- 多选列 --> <!-- v-if: 当 multipleSelection 为 true 时显示多选列 --> <!-- selectable: 当开启单选模式时,使用 checkSelectable 方法判断是否可选 --> <!-- type: 列的类型,设置为 'selection' 表示多选列 --> <!-- width: 列的宽度 --> <el-table-column v-if="multipleSelection" :selectable="singleSelect ? checkSelectable : undefined" type="selection" width="40" ></el-table-column> <!-- 序号列 --> <!-- v-if: 当 showIndex 为 true 时显示序号列 --> <!-- align: 列内容的对齐方式,设置为居中对齐 --> <!-- label: 列的表头名称 --> <!-- width: 列的宽度 --> <el-table-column v-if="showIndex" align="center" label="序号" width="60" > <!-- 序号列的内容模板 --> <!-- 计算并显示当前的序号,考虑分页因素 --> <template #default="scope"> {{ (currentPage - 1) * pageSize + scope.$index + 1 }} </template> </el-table-column> <!-- 动态列 --> <!-- v-for: 遍历 columns 数组,动态生成列 --> <!-- key: 为每个列设置唯一的 key 值 --> <!-- formatter: 列的格式化函数 --> <!-- label: 列的表头名称 --> <!-- min-width: 列的最小宽度 --> <!-- prop: 列对应的数据字段 --> <!-- width: 列的宽度 --> <!-- show-overflow-tooltip: 是否在内容溢出时显示 tooltip --> <el-table-column v-for="column in columns" :key="column.prop" :formatter="column.formatter" :label="column.label" :min-width="column.minWidth || '80'" :prop="column.prop" :width="column.width" v-if="!isColumnHidden(column)" :show-overflow-tooltip="true" :fixed="column.fixed" > <!-- 动态列的内容模板 --> <template #default="scope"> <!-- Element UI 提示框组件 --> <!-- disabled: 是否禁用提示框 --> <!-- effect: 提示框的样式效果 --> <!-- placement: 提示框的位置 --> <el-tooltip :disabled="!isTextOverflow(scope.row[column.prop], column.className)" :effect="effect" style="max-width: 600px" :placement="placement" > <!-- 提示框的内容模板 --> <template #content> <div style="max-width: 600px; word-break: break-all">{{ scope.row[column.prop] }}</div> </template> <!-- 显示单元格内容的容器 --> <!-- class: 根据 column.preview 的值动态添加 'text-ellipsis' 类名 --> <!-- style: 设置文本颜色和鼠标指针样式 --> <!-- @click: 单元格点击事件 --> <div :class="{'text-ellipsis': typeof column.preview === 'function' ? column.preview(scope.row) : column.preview}" :style="{ color: getTextColor(scope.row, column), cursor: (typeof column.clickable === 'function' ? column.clickable(scope.row) : column.clickable) ? 'pointer' : 'default' // 新增样式 }" class="preview-text" @click="handleCellClick(column, scope.row)" > <!-- 显示单元格内容 --> <!-- v-html: 根据 column.formatter 的值动态显示内容 --> <div :class="column.className" v-html="column.formatter ? column.formatter(scope.row, column, scope.row[column.prop]) : scope.row[column.prop]" v-if="!column.isInlineViewing"></div> <!-- 内联查看图标 --> <!-- v-if: 当 column.isInlineViewing 为 true 时显示图标 --> <div v-if="column.isInlineViewing"> <el-button type="text" @click="handleAction(column.icon, scope.row)"> <!-- 操作按钮的内容 --> <div class="action-item"> <!-- 操作按钮的图标 --> <svg-icon :icon-class="column.icon"/> <!-- 操作按钮的文本 --> <div class="action-text" v-html="column.formatter ? column.formatter(scope.row, column, scope.row[column.prop]) : scope.row[column.prop]"></div> <!-- <span class="action-text">{{ scope.row[column.prop] }}</span>--> </div> </el-button> </div> <!-- 新增预览图标 --> <!-- v-if: 根据 column.preview 的值判断是否显示图标 --> <svg-icon v-if="typeof column.preview === 'function' ? column.preview(scope.row) : column.preview" class="preview-icon" icon-class="primary" /> </div> </el-tooltip> </template> </el-table-column> <!-- 操作列 --> <!-- v-if: 当 showActionColumn 为 true 时显示操作列 --> <!-- label: 操作列的表头名称 --> <!-- width: 操作列的宽度 :width="actionColumnWidth"--> <!-- fixed: 操作列固定在右侧 --> <el-table-column v-if="showActionColumn" :label="operation" :min-width="calculateActionColumnWidth()" fixed="right" > <!-- 操作列的内容模板 --> <template #default="scope"> <!-- 操作按钮容器 --> <div class="operation"> <!-- 遍历操作按钮数组,动态生成按钮 --> <!-- v-for: 遍历操作按钮数组 --> <!-- key: 为每个按钮设置唯一的 key 值 --> <!-- size: 按钮的大小 --> <!-- type: 按钮的类型 --> <!-- @click: 按钮点击事件 --> <el-button v-for="button in (scope.row.actionButtons || actionButtons)" :key="button.name" size="small" type="text" :loading="typeof button.loading === 'function' ? button.loading(scope.row) : button.loading" v-if="!button.hasPermission || $auth.hasPermi(button.hasPermission)" :disabled="button.disabled ? (typeof button.disabled === 'function' ? button.disabled(scope.row) : button.disabled) : false" @click.stop="handleAction(button.name, scope.row)" > <!-- 操作按钮的内容 --> <span class="action-item"> <!-- 操作按钮的图标 --> <svg-icon :icon-class="button.icon || button.name" class="action-icon" :class="{'disabled-text': button.disabled? (typeof button.disabled === 'function' ? button.disabled(scope.row) : button.disabled) : false}" /> <!-- 操作按钮的文本 --> <span class="action-text" :class="{'disabled-text': button.disabled? (typeof button.disabled === 'function' ? button.disabled(scope.row) : button.disabled) : false}">{{ button.label }}</span> </span> </el-button> </div> </template> </el-table-column> </el-table> </div> <!-- 分页组件容器 --> <div class="pagination-wrapper" v-if="paginationFlag"> <!-- Element UI 分页组件 --> <!-- background: 是否显示分页按钮的背景 --> <!-- current-page: 当前页码 --> <!-- layout: 分页组件的布局 --> <!-- page-size: 每页显示的记录数 --> <!-- page-sizes: 每页显示记录数的可选值 --> <!-- total: 总记录数 --> <!-- @size-change: 每页显示记录数变化时触发的事件 --> <!-- @current-change: 当前页码变化时触发的事件 --> <el-pagination :background="background" :current-page="currentPage" :layout="layout" :page-size="pageSize" :page-sizes="pageSizes" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" /> </div> </div> </template> <script> /** * ElementTableWrapper 组件 * 封装了 Element UI 的表格组件,提供了多选、序号、动态列、操作列和分页等功能 */ export default { name: 'ElementTableWrapper', props: { // 表格数据 tableData: { type: Array, default: () => [] }, // 列配置 columns: { type: Array, default: () => [] }, // 列配置 operation: { type: String, default: '操作' }, // 是否开启多选功能 multipleSelection: { type: Boolean, default: false }, // 是否显示操作列 showActionColumn: { type: Boolean, default: true }, // 是否显示边框 border: { type: Boolean, default: true }, // 是否显示斑马纹 stripe: { type: Boolean, default: false }, // 是否亮当前 highlightCurrentRow: { type: Boolean, default: false }, // 是否亮当前 dialogTable: { type: Boolean, default: false }, // 是否显示分页功能 paginationFlag: { type: Boolean, default: true }, // 样式类名回调函数 rowClassName: { type: Function, default: () => '' }, // 操作按钮配置 actionButtons: { type: Array, default: () => [ {name: 'add', label: '新增', icon: 'add'}, {name: 'edit', label: '编辑', icon: 'edit'}, {name: 'delete', label: '删除', icon: 'delete'} ] }, // 表格头背景颜色 headerBgColor: { type: String, default: '#F9FAFB' }, // 表格边框颜色 tableBorderColor: { type: String, default: 'rgb(234, 236, 240)' }, // 新增单选控制props singleSelect: { // 启用单选模式 type: Boolean, default: false }, // 默认选中首 defaultSelect: { type: Boolean, default: false }, // 新增分页相关props currentPage: { type: Number, default: 1 }, // 每页显示的记录数 pageSize: { type: Number, default: 10 }, // 总记录数 total: { type: Number, default: 0 }, // 每页显示记录数的可选值 pageSizes: { type: Array, default: () => [10, 20, 50, 100] }, // 分页组件的布局 layout: { type: String, default: 'total, sizes, prev, pager, next, jumper' }, // 是否显示分页按钮的背景 background: { type: Boolean, default: true }, // 表格度 tableHeight: { type: [String, Number], default: '100%' // 默认100%度 }, // 是否显示序号列 showIndex: { type: Boolean, default: true }, // 是否启用点击事件 rowClickEnable: { type: Boolean, default: false }, // 点击事件处理器 rowClickHandler: { type: Function, default: null }, // 提示框的样式效果 effect: { type: String, default: 'dark' }, // 提示框的位置 placement: { type: String, default: 'top' }, enableRowClickSelect: { type: Boolean, default: false }, // 新增 loading 属性 loading: { type: Boolean, default: false } }, data() { return { currentSelection: null, // 当前选中 crossPageSelection: [], // 跨页选择的数据 } }, watch: { /** * 监听表格数据变化 * 当表格数据变化且 defaultSelect 为 true 时,默认选中第一 * @param {Array} newVal - 新的表格数据 */ tableData(newVal) { if (this.defaultSelect && newVal.length > 0) { this.$nextTick(() => { // 切换第一的选中状态为选中 this.$refs.table.toggleRowSelection(newVal[0], true) }) } }, }, mounted() { this.$emit('rendered'); // 触发 rendered 事件 }, methods: { /** * 处理操作按钮点击事件 * 触发与按钮名称对应的自定义事件,并传递当前数据 * @param {string} actionName - 操作按钮的名称 * @param {Object} row - 当前的数据 */ handleAction(actionName, row) { this.$emit(actionName, row); }, // 处理点击事件 handleRowClick(row) { if (this.rowClickEnable) { // 触发自定义点击事件 this.$emit('row-click', row); // 如果有自定义处理函数则执 if (this.rowClickHandler) { this.rowClickHandler(row); } } }, /** * 获取文本颜色 * 根据列的文本颜色规则,判断是否需要修改文本颜色 * @param {Object} row - 当前的数据 * @param {Object} column - 当前列的配置 * @returns {string} - 文本颜色 */ getTextColor(row, column) { // 将参数名从prop改为column if (column.textColorRule) { const {condition, color} = column.textColorRule; if (condition(row)) { // 新增:如果 color 是函数则调用并传入当前数据,否则直接使用字符串 return typeof color === 'function' ? color(row) : color; } } return 'rgb(38, 38, 38)'; }, /** * 处理单元格点击事件 * 当单元格可点击时,触发 'cell-click' 自定义事件 * @param {Object} column - 当前列的配置 * @param {Object} row - 当前的数据 */ handleCellClick(column, row) { if (typeof column.clickable === 'function' ? column.clickable(row) : column.clickable) { this.$emit('cell-click', { column, row, prop: column.prop }) } }, /** * 判断文本是否溢出 * 通过比较元素的滚动宽度和客户端宽度来判断文本是否溢出 * @param {string} text - 文本内容 * @param className * @returns {boolean} - 文本是否溢出 */ isTextOverflow(text, className) { if (!text || !className) return false; // 创建测量容器 const container = document.createElement('div'); container.className = className; container.style.cssText = ` position: absolute; visibility: hidden; white-space: nowrap; font-size: 14px; padding: 0 8px; max-width: 100%; `; // 创建文本测量元素 const textSpan = document.createElement('div'); textSpan.textContent = text; // 测量逻辑 document.body.appendChild(container); container.appendChild(textSpan); const containerWidth = container.offsetWidth; const textWidth = textSpan.offsetWidth; document.body.removeChild(container); // 双重判断:文本超过容器宽度且在表格最大宽度范围内 return textWidth > containerWidth; }, /** * 处理每页显示记录数变化事件 * 触发 'update:pageSize' 和 'pagination-change' 自定义事件 * @param {number} val - 新的每页显示记录数 */ handleSizeChange(val) { this.$emit('update:pageSize', val); this.$emit('pagination-change', { page: this.currentPage, size: val }); // 新增:滚动到表格顶部 this.$nextTick(() => { const tableWrapper = this.$refs.table.$el.querySelector('.el-table__body-wrapper'); if (tableWrapper) { tableWrapper.scrollTop = 0; } }); }, // 暴露给父组件的方法 toggleRowSelection(row, selected) { this.$refs.table.toggleRowSelection(row, selected) }, /** * 处理表格选中项变化事件 * 当开启单选模式时,确保只选中一 * @param {Array} selection - 当前选中的数据数组 */ handleSelectionChange(selection) { if (this.singleSelect) { if (selection.length > 1) { const last = selection[selection.length - 1] // 清空所有选中项 this.$refs.table.clearSelection() // 切换最后一项的选中状态为选中 this.$refs.table.toggleRowSelection(last) this.currentSelection = last } else { this.currentSelection = selection[0] || null } } // 触发 'selection-change' 自定义事件,传递跨页选择的数据 this.$emit('selection-change', selection) }, /** * 计算操作列的宽度 * 根据操作按钮的数量和每个按钮的大致宽度计算操作列的宽度 * @returns {number} - 操作列的宽度 */ calculateActionColumnWidth() { const getButtonWidth = (text) => { if (text.length >= 4) return 80; else if (text.length >= 2) return 60; return 40; }; // 权限判断方法(与模板中的v-if逻辑保持一致) const hasPermission = (button) => { return !button.hasPermission || this.$auth.hasPermi(button.hasPermission); }; const maxButtonCountRow = this.tableData.reduce((prev, current) => { // 过滤无权限按钮后比较 const prevValid = (prev.actionButtons || this.actionButtons).filter(hasPermission); const currentValid = (current.actionButtons || this.actionButtons).filter(hasPermission); return prevValid.length > currentValid.length ? prev : current; }, {}); // 过滤有效按钮 const validButtons = (maxButtonCountRow.actionButtons || this.actionButtons).filter(hasPermission); return validButtons.reduce((sum, button) => { return sum + getButtonWidth(button.label); }, 0); // 增加安全边距 }, /** * 检查是否可选 * 当开启单选模式时,判断是否可以被选中 * @param {Object} row - 当前的数据 * @param {number} index - 当前的索引(未使用) * @returns {boolean} - 是否可选 */ checkSelectable(row, index) { return !this.currentSelection || row === this.currentSelection }, /** * 处理当前页码变化事件 * 触发 'update:currentPage' 和 'pagination-change' 自定义事件 * @param {number} val - 新的当前页码 */ handleCurrentChange(val) { this.$emit('update:currentPage', val); this.$emit('pagination-change', { page: val, size: this.pageSize }); // 新增:滚动到表格顶部 this.$nextTick(() => { const tableWrapper = this.$refs.table.$el.querySelector('.el-table__body-wrapper'); if (tableWrapper) { tableWrapper.scrollTop = 0; } }); }, // 新增列隐藏判断方法 isColumnHidden(column) { if (typeof column.hidden === 'function') { return column.hidden(this.$parent) // 传入父组件上下文 } return !!column.hidden }, /** * 处理点击选中事件 * 当 enableRowClickSelect 为 true 时,选中点击 * @param {Object} row - 当前点击的数据 */ handleRowClickSelect(row) { if (this.enableRowClickSelect) { if (this.singleSelect) { // 单选模式,清空其他选中项,选中当前 this.$refs.table.clearSelection(); this.$refs.table.toggleRowSelection(row, true); } else { // 多选模式,切换当前的选中状态 this.$refs.table.toggleRowSelection(row); } this.$emit('selection-change', this.$refs.table.selection); } } } }; </script> <style lang="scss" scoped> .table-box { display: flex; flex-direction: column; flex: 1; overflow: hidden; } .table-container { flex: 1; overflow: auto; ::v-deep .el-table { .el-table__body-wrapper { overflow: auto; height: calc(100% - 44px) !important; } } } /** * 对话框中的表格样式 * 设置表格度为 100% */ .dialog-table { height: 100%; } /** * 分页组件容器样式 * 绝对定位到底部右侧,宽度为表格宽度减去 33px,文本右对齐 */ .pagination-wrapper { position: relative; text-align: right; margin-bottom: 10px; margin-top: 15px; } /** * 表格容器样式 * 确保表格容器有相对定位,设置最小度 */ div:first-child { position: relative; min-height: 30px; /* 可根据需要调整最小度 */ } /** * 分页按钮样式 * 设置分页按钮的边框、圆角、内边距和鼠标悬停样式 */ ::v-deep .el-pagination.is-background { .btn-prev, .btn-next { border: 1px solid #DCDFE6; border-radius: 4px; padding: 0 8px; margin: 0 5px; &:hover { color: #409EFF; border-color: #409EFF; } } } /** * 表格基础样式 * 设置表格字体大小,处理文本换和溢出情况,设置预览图标样式 */ ::v-deep .el-table { font-size: 14px; /** * 允许换的列样式 * 允许文本换,调整 */ .wrap-text-column { white-space: normal !important; /* 允许换 */ word-break: break-all; /* 允许单词内换 */ line-height: 1.5; /* 调整 */ } /** * 鼠标悬停在表格上时的样式 * 显示预览图标 */ tr:hover { .preview-icon { display: inline-block !important; } } .cell-content { .content-wrapper { .el-icon-loading { display: inline-block !important; position: absolute; right: 8px; top: 25%; color: #1D9C9D; } } } /** * 非换列的样式 * 文本不换,溢出部分显示省略号 */ :not(.wrap-text-column) { .cell { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } } /** * 文本省略样式 * 文本不换,溢出部分显示省略号,为图标留出空间 */ .text-ellipsis { position: relative; padding-right: 24px; // 为图标留出空间 display: inline-block; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } /** * 预览图标样式 * 默认隐藏,鼠标悬停在表格上时显示 */ .preview-icon { display: none; position: absolute; right: 8px; top: 25%; cursor: pointer; color: #1D9C9D; } /** * 预览文本样式 * 文本不换,溢出部分显示省略号,垂直居中 */ .preview-text { display: inline-block; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: middle; line-height: 2; /** * 标签列样式 * 允许标签换设置标签样式 */ .tags-column { white-space: normal !important; line-height: 1.5; /** * 标签项样式 * 设置标签的边框、圆角、颜色和背景颜色 */ .tag-item { display: inline-block; padding: 2px 8px; margin: 2px; border: 1px solid rgba(255, 105, 41, 0.5); border-radius: 4px; color: #FF6929; background-color: transparent; font-size: 12px; word-break: keep-all; /* 防止单个标签换 */ } } } /** * 表格表头和单元格样式 * 调整内边距,设置文本不换,不隐藏溢出内容,不显示省略号 */ th, td { padding: 2px 0; .cell { white-space: nowrap; overflow: visible; text-overflow: ellipsis; line-height: 1; } } td.el-table__cell div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } } /** * 操作项样式 * 设置操作按钮和操作项的布局为水平居中对齐 */ .operation, .action-item { display: flex; white-space: nowrap; /* 防止操作按钮 */ align-items: center; } /** * 文本省略样式 * 文本不换,溢出部分显示省略号 */ .text-ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .action-item { cursor: pointer; margin-right: 8px; &:last-child { margin-right: 0; } .action-text { margin-left: 4px; font-size: 12px; color: rgb(31, 198, 234); } .action-icon { width: 1em; height: 1em; color: #1FC6EA; transition: color 0.2s; &:hover { color: #1FC6EA; } } .disabled-text { color: #999; } } ::v-deep .el-button--text { padding: 2px 0; } .action-item { display: inline-flex; align-items: center; cursor: pointer; margin-right: 8px; &:last-child { margin-right: 0; } } .action-text { margin-left: 4px; font-size: 12px; color: #606266; } .action-icon { width: 1em; height: 1em; cursor: pointer; color: #606266; transition: color 0.2s; } .action-icon:hover { color: #1FC6EA; } /* 添加表格字体大小设置 */ ::v-deep .el-table { font-size: 14px; /* 可调整字体大小 */ } ::v-deep .el-table th, ::v-deep .el-table td { padding: 10px 0; /* 调整单元格内边距 */ } /* 表格内容单显示 */ ::v-deep .el-table .cell { white-space: nowrap; /* 不换 */ overflow: visible; /* 不隐藏溢出内容 */ text-overflow: ellipsis; /* 不显示省略号 */ line-height: 1; /* */ } </style> 数据内容{ "total": 77251, "rows": [ { "id": "3109226059753587717", "stationId": "10042", "stationName": "盘城变", "voltageLevel": "220kV", "voltageLevelCode": "33", "bayId": "10006395", "bayName": "2号主变", "areaId": "206", "areaName": "220北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "2号主变110kV侧中性点成套装置", "equipNo": "10MZ0108517959259", "equipModel": "SR-JXB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户外式", "useEnvironmentCode": "2", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "江苏省如压电器有限公司", "startTime": "2022-09-30", "manufactureDate": "2021-05-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587718", "stationId": "10089", "stationName": "万寿变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "10004149", "bayName": "1号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "1号主变中性点成套装置", "equipNo": "10MZ0108518228845", "equipModel": "CG-IXB-110/1600", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户外式", "useEnvironmentCode": "2", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "湖南长压开关有限公司", "startTime": "2023-01-01", "manufactureDate": "2021-11-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587719", "stationId": "64386cb90e3c11ee90179e25b59ef9e0", "stationName": "绿洲变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "64d4ac720e3c11ee90179e25b59ef9e0", "bayName": "1号主变", "areaId": "210", "areaName": "城南", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "1号主变中性点成套装置", "equipNo": "10MZ0108519189286", "equipModel": "XK-ZJB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "西安西电压开关有限责任公司", "startTime": "2023-06-26", "manufactureDate": "2023-05-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587720", "stationId": "64386cb90e3c11ee90179e25b59ef9e0", "stationName": "绿洲变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "64d4ac7c0e3c11ee90179e25b59ef9e0", "bayName": "2号主变", "areaId": "210", "areaName": "城南", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "2号主变中性点成套装置", "equipNo": "10MZ0108519190392", "equipModel": "XK-ZJB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "西安西电压开关有限责任公司", "startTime": "2023-06-26", "manufactureDate": "2023-05-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587721", "stationId": "10000002", "stationName": "徐庄变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "128246796", "bayName": "2号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "2号主变中性点成套装置", "equipNo": "10MZ0108519046377", "equipModel": "GW13-72.5(W)", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "江苏如皋压电器有限公司", "startTime": "2011-03-11", "manufactureDate": "2011-01-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587722", "stationId": "10000002", "stationName": "徐庄变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "10014690", "bayName": "1号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "1号主变中性点成套装置", "equipNo": "10MZ0108519046388", "equipModel": "GW13-72.5(W)", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "江苏如皋压电器有限公司", "startTime": "2011-03-11", "manufactureDate": "2011-01-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587723", "stationId": "a13ce0756f8a06deed5382a7950153a13cbdc630ab", "stationName": "诚实变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "c2b10909-afe1-47fa-af68-c268247d1a7d", "bayName": "2号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "2号主变中性点成套装置", "equipNo": "10MZ0108521242648", "equipModel": "GW13-126W/1600", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "常州思源东芝变压器有限公司", "startTime": "2024-09-06", "manufactureDate": "2024-01-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587724", "stationId": "19b7e87c-70b3-4736-9126-066604cfe72f", "stationName": "小村变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "d22b61f9-4a32-4da2-b2d8-c10a447d1f51", "bayName": "2号主变", "areaId": "210", "areaName": "城南", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "2号主变中性点成套装置", "equipNo": "10MZ0108521256500", "equipModel": "ZJKK-320-15", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "江苏如压电器有限公司", "startTime": "2024-09-23", "manufactureDate": "2024-02-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587725", "stationId": "19b7e87c-70b3-4736-9126-066604cfe72f", "stationName": "小村变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "6acbf7ba-0563-4500-92af-07f80a9f0137", "bayName": "1号主变", "areaId": "210", "areaName": "城南", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "1号主变中性点成套装置", "equipNo": "10MZ0108521256499", "equipModel": "ZJKK-320-15", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "江苏如压电器有限公司", "startTime": "2024-09-23", "manufactureDate": "2024-02-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587726", "stationId": "10055", "stationName": "珠江路变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "4fcb2a97-9e97-4382-ad72-76fa14a0a900", "bayName": "3号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "3号主变中性点成套装置", "equipNo": "10MZ0108521889218", "equipModel": "SR-JXB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "3号主变中性点成套装置", "useEnvironment": "户外式", "useEnvironmentCode": "2", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "KH250066", "produceOrgId": "", "produceOrg": "江苏如压电气有限公司", "startTime": "2025-04-01", "manufactureDate": "2025-03-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587727", "stationId": "10054", "stationName": "云南路变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "10001660", "bayName": "1号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "1号主变中性点成套装置", "equipNo": "10MZ0108521404169", "equipModel": "XK-ZJB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "2801240138", "produceOrgId": "", "produceOrg": "西安西电压开关有限责任公司", "startTime": "2024-10-23", "manufactureDate": "2024-08-30", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587728", "stationId": "0f1da7c8-e937-47a9-ae19-485c64ed19bd", "stationName": "骆家变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "09cf5ce2-3384-4b4c-920d-d782477a447f", "bayName": "2号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "2号主变中性点成套装置", "equipNo": "10MZ0108521505000", "equipModel": "SR-JXB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "江苏省如皋压电器有限公司", "startTime": "2024-12-20", "manufactureDate": "2023-10-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587729", "stationId": "0f1da7c8-e937-47a9-ae19-485c64ed19bd", "stationName": "骆家变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "6ab30fc2-7c0f-4c53-8ee1-0e5b5745ec71", "bayName": "1号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "1号主变中性点成套装置", "equipNo": "10MZ0108521504999", "equipModel": "SR-JXB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "江苏省如皋压电器有限公司", "startTime": "2024-12-20", "manufactureDate": "2023-10-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587730", "stationId": "10054", "stationName": "云南路变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "10001661", "bayName": "2号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "2号主变中性点成套装置", "equipNo": "10MZ0108521500656", "equipModel": "XK-ZJB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "60613", "produceOrgId": "", "produceOrg": "西安西电压开关有限公司", "startTime": "2024-12-19", "manufactureDate": "2006-06-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587731", "stationId": "10100", "stationName": "上新河变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "10005161", "bayName": "2号主变", "areaId": "210", "areaName": "城南", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "2号主变中性点成套装置", "equipNo": "10MZ0108522469177", "equipModel": "SR-JXB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户外式", "useEnvironmentCode": "2", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "KH250036828", "produceOrgId": "", "produceOrg": "江苏如压电器有限公司", "startTime": "2025-05-07", "manufactureDate": "2025-03-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587732", "stationId": "10055", "stationName": "珠江路变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "8bbba0e3-8214-4833-80c2-4fbb37cedd47", "bayName": "2号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "2号主变中性点成套装置", "equipNo": "10MZ0108522399716", "equipModel": "SR-JXB-110", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "KH250064", "produceOrgId": "", "produceOrg": "江苏如压电器有限公司", "startTime": "2025-05-06", "manufactureDate": "2025-03-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109226059753587733", "stationId": "a13ce0756f8a06deed5382a7950153a13cbdc630ab", "stationName": "诚实变", "voltageLevel": "110kV", "voltageLevelCode": "32", "bayId": "9b15d344-3e5b-41f1-88fb-86f94ec6f5c3", "bayName": "1号主变", "areaId": "213", "areaName": "城北", "equipLevel": "32", "equipLevelName": "110kV", "equipName": "1号主变中性点成套装置", "equipNo": "10MZ0108522479005", "equipModel": "GW13-126W/1600", "equipTypeId": 141, "equipTypeName": "主变中性点成套装置", "transRunNo": "", "useEnvironment": "户内式", "useEnvironmentCode": "1", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": null, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "", "produceOrgId": "", "produceOrg": "江苏华鹏变压器有限公司", "startTime": "2025-05-12", "manufactureDate": "2025-02-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": "O", "phaseCode": "4", "psrStateName": "在运", "psrState": "20" }, { "id": "3109222058689561600", "stationId": "154178358", "stationName": "滨南变", "voltageLevel": "220kV", "voltageLevelCode": "33", "bayId": null, "bayName": null, "areaId": "203", "areaName": "220南", "equipLevel": "22", "equipLevelName": "10kV", "equipName": "1号主变102B4接地刀闸", "equipNo": "10MZ0108517740354", "equipModel": null, "equipTypeId": 138, "equipTypeName": "低压隔离开关", "transRunNo": "102B4", "useEnvironment": "", "useEnvironmentCode": "", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": 4000.0, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "C2113022", "produceOrgId": "", "produceOrg": "江苏如压电器有限公司", "startTime": "2022-06-30", "manufactureDate": "2022-07-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": null, "phaseCode": null, "psrStateName": "在运", "psrState": "20" }, { "id": "3109222058689561601", "stationId": "154178358", "stationName": "滨南变", "voltageLevel": "220kV", "voltageLevelCode": "33", "bayId": null, "bayName": null, "areaId": "203", "areaName": "220南", "equipLevel": "22", "equipLevelName": "10kV", "equipName": "1号主变102B5接地刀闸", "equipNo": "10MZ0108517740480", "equipModel": null, "equipTypeId": 138, "equipTypeName": "低压隔离开关", "transRunNo": "102B5", "useEnvironment": "", "useEnvironmentCode": "", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": 4000.0, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "C2113021", "produceOrgId": "", "produceOrg": "江苏如压电器有限公司", "startTime": "2022-06-30", "manufactureDate": "2022-07-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": null, "phaseCode": null, "psrStateName": "在运", "psrState": "20" }, { "id": "3109222058689561602", "stationId": "154178358", "stationName": "滨南变", "voltageLevel": "220kV", "voltageLevelCode": "33", "bayId": null, "bayName": null, "areaId": "203", "areaName": "220南", "equipLevel": "22", "equipLevelName": "10kV", "equipName": "2号主变102A4接地刀闸", "equipNo": "10MZ0108517740549", "equipModel": null, "equipTypeId": 138, "equipTypeName": "低压隔离开关", "transRunNo": "102A4", "useEnvironment": "", "useEnvironmentCode": "", "structType": "", "structTypeCode": "", "insulationMedium": "", "insulationMediumCode": "", "ratedCurrent": 4000.0, "ratedBreakCurrent": null, "ratedCapacity": null, "breakerStructType": "", "breakerStructTypeCode": "", "ownNo": "C2113023", "produceOrgId": "", "produceOrg": "江苏如压电器有限公司", "startTime": "2022-06-30", "manufactureDate": "2022-07-01", "lastUpdateTime": "", "nextUpdateTime": "", "cycle": "6", "cycleUnit": "1", "dispatchRecord": "", "regulatePressureMode": null, "regulatePressureModeCode": null, "equipmentType": null, "equipmentTypeCode": null, "phase": null, "phaseCode": null, "psrStateName": "在运", "psrState": "20" } ], "code": 200, "msg": "查询成功" } 在this.tableData = res.rows.map(row => { const deviceType = row.equipTypeName; const deviceColumns = this.configMap[deviceType] || []; const newRow = {...row}; // 遍历所有列,若不在设备类型对应列中,则添加斜线 this.columns.forEach(col => { const prop = col.prop; if (!deviceColumns.includes(col.label)) { newRow[prop] = '/'; } }); return newRow; });为什么渲染的时候需要2s左右
07-26
将以下代码做成PDF教案,详述每段代码的作用,包括整个程序的架构 import tkinter as tk from tkinter import filedialog, messagebox from tkinter import ttk import pandas as pd import json import os import sys class DataAnalysisApp: def __init__(self, root): self.root = root self.root.title("数据分析助手") # 配置文件路径 self.config_file = self.get_resource_path("app_config.json") # 加载配置 self.config = self.load_config() # 设置窗口大小和位置 window_width = self.config.get("window_width", 1000) window_height = self.config.get("window_height", 600) screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() center_x = int(screen_width/2 - window_width/2) center_y = int(screen_height/2 - window_height/2) self.root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}') # 设置窗口最小尺寸 self.root.minsize(800, 400) # 创建菜单栏 self.menu_bar = tk.Menu(self.root) # 文件菜单 self.file_menu = tk.Menu(self.menu_bar, tearoff=0) self.file_menu.add_command(label="打开", command=self.open_file) self.file_menu.add_separator() self.file_menu.add_command(label="退出", command=self.root.quit) self.menu_bar.add_cascade(label="文件", menu=self.file_menu) # 添加公式菜单 self.formula_menu = tk.Menu(self.menu_bar, tearoff=0) self.formula_menu.add_command(label="自定义公式", command=self.open_formula_window) self.menu_bar.add_cascade(label="公式", menu=self.formula_menu) self.root.config(menu=self.menu_bar) # 创建主框架 self.main_frame = ttk.Frame(self.root) self.main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=5) # 创建可拖拽的分隔窗口 self.paned_window = ttk.PanedWindow(self.main_frame, orient=tk.HORIZONTAL) self.paned_window.pack(fill=tk.BOTH, expand=True) # 创建左侧数据显示区域 self.left_frame = ttk.Frame(self.paned_window) # 创建右侧控制面板 self.right_frame = ttk.Frame(self.paned_window) # 添加框架到分隔窗口 self.paned_window.add(self.left_frame, weight=1) self.paned_window.add(self.right_frame, weight=0) # 设置分隔位置 if "paned_position" in self.config: self.paned_window.after(100, lambda: self.paned_window.sashpos(0, self.config["paned_position"])) # 创建算法选择区域 self.algorithm_frame = ttk.LabelFrame(self.right_frame, text="算法选择", padding=10) self.algorithm_frame.pack(fill=tk.X, pady=(0, 10)) # 添加算法选择下拉框 self.algorithm_var = tk.StringVar() self.algorithms = [ "描述性统计", "相关性分析", "数据分布分析", "时间序列分析", "分组统计分析", "缺失值分析", "CPK分析" ] self.algorithm_combo = ttk.Combobox( self.algorithm_frame, textvariable=self.algorithm_var, values=self.algorithms, state="readonly" ) self.algorithm_combo.pack(fill=tk.X, pady=(5, 0)) self.algorithm_combo.set("请选择分析方法") # 添加运按钮 self.run_button = ttk.Button( self.algorithm_frame, text="运分析", command=self.run_analysis ) self.run_button.pack(fill=tk.X, pady=(10, 0)) # 创建结果显示区域 self.result_frame = ttk.LabelFrame(self.right_frame, text="分析结果", padding=10) self.result_frame.pack(fill=tk.BOTH, expand=True) # 添加结果文本框 self.result_text = tk.Text( self.result_frame, wrap=tk.WORD, width=30, height=20, font=('Arial', 10) # 设置字体 ) # 为结果文本框添加滚动条 self.result_scrollbar = ttk.Scrollbar( self.result_frame, orient="vertical", command=self.result_text.yview ) # 正确放置滚动条和文本框 self.result_scrollbar.pack(side=tk.RIGHT, fill=tk.Y) self.result_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 配置文本框的滚动 self.result_text.configure(yscrollcommand=self.result_scrollbar.set) # 配置文本标签样式 self.result_text.tag_configure('header', font=('Arial', 11, 'bold')) self.result_text.tag_configure('subtitle', font=('Arial', 10, 'bold')) self.result_text.tag_configure('warning', foreground='orange') self.result_text.tag_configure('error', foreground='red') # 设置为只读 self.result_text.config(state='disabled') # 创建框架来容纳Treeview和滚动条 self.tree_frame = ttk.Frame(self.left_frame) self.tree_frame.pack(fill=tk.BOTH, expand=True) # 创建并配置Treeview样式 style = ttk.Style() style.configure("Treeview", rowheight=22, # 稍微减小 font=('Arial', 9), # 更改字体大小 background="#FFFFFF", fieldbackground="#FFFFFF", foreground="#000000", borderwidth=1, relief='solid' ) # 配置标题样式,更接近Excel style.configure("Treeview.Heading", font=('Arial', 9, 'bold'), relief='flat', borderwidth=1, background='#F0F0F0', # Excel风格的标题背景色 foreground='#000000' ) # 设置选中颜色为Excel风格的蓝色 style.map('Treeview', background=[('selected', '#E1E9F5')], # Excel选中的浅蓝色 foreground=[('selected', '#000000')] # 选中时保持黑色文字 ) # 设置Treeview网格线颜色 style.configure("Treeview", background="white", fieldbackground="white", foreground="black", bordercolor="#DDD", # 网格线颜色 lightcolor="#DDD", # 亮边框颜色 darkcolor="#DDD" # 暗边框颜色 ) # 创建Treeview控件用于显示数据 self.tree = ttk.Treeview(self.tree_frame) # 创建垂直滚动条 self.vsb = ttk.Scrollbar(self.tree_frame, orient="vertical", command=self.tree.yview) self.vsb.pack(side=tk.RIGHT, fill=tk.Y) # 创建水平滚动条 self.hsb = ttk.Scrollbar(self.tree_frame, orient="horizontal", command=self.tree.xview) self.hsb.pack(side=tk.BOTTOM, fill=tk.X) # 设置Treeview的滚动 self.tree.configure(yscrollcommand=self.vsb.set, xscrollcommand=self.hsb.set) # 放置Treeview self.tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 显示标题 self.tree["show"] = "headings" # 创建状态栏 self.status_bar = ttk.Label(self.root, text="就绪", anchor=tk.W) self.status_bar.pack(side=tk.BOTTOM, fill=tk.X, padx=5, pady=3) # 添加数据存储变量 self.current_data = None def run_analysis(self): if self.current_data is None: messagebox.showwarning("警告", "请先加载数据") return selected_algorithm = self.algorithm_var.get() if selected_algorithm == "请选择分析方法": messagebox.showwarning("警告", "请选择分析方法") return try: # 创建不包含前两列的数据副本 analysis_data = self.current_data.iloc[:, 2:].copy() if analysis_data.empty: messagebox.showwarning("警告", "没有可分析的数据列") return self.result_text.config(state='normal') self.result_text.delete(1.0, tk.END) if selected_algorithm == "CPK分析": self._run_cpk_analysis(analysis_data) elif selected_algorithm == "描述性统计": self._run_descriptive_analysis(analysis_data) elif selected_algorithm == "相关性分析": self._run_correlation_analysis(analysis_data) elif selected_algorithm == "数据分布分析": self._run_distribution_analysis(analysis_data) elif selected_algorithm == "时间序列分析": self._run_time_series_analysis(analysis_data) elif selected_algorithm == "分组统计分析": self._run_group_analysis(analysis_data) elif selected_algorithm == "缺失值分析": self._run_missing_value_analysis(analysis_data) self.result_text.config(state='disabled') except Exception as e: self.result_text.delete(1.0, tk.END) self.result_text.insert(tk.END, f"⚠ 分析过程出错:\n{str(e)}", 'error') self.result_text.config(state='disabled') def _run_descriptive_analysis(self, data): """描述性统计""" numeric_cols = data.select_dtypes(include=['int64', 'float64']).columns non_numeric_cols = data.select_dtypes(exclude=['int64', 'float64']).columns # 处理数值列 if not numeric_cols.empty: numeric_stats = data[numeric_cols].describe() self.result_text.insert(tk.END, "═══ 数值型数据统计 ═══\n\n", 'header') # 格式化数值统计结果 for col in numeric_cols: stats = numeric_stats[col] self.result_text.insert(tk.END, f"▶ {col}\n", 'subtitle') self.result_text.insert(tk.END, f" • 数量: {stats['count']:.0f}\n") self.result_text.insert(tk.END, f" • 均值: {stats['mean']:.2f}\n") self.result_text.insert(tk.END, f" • 标准差: {stats['std']:.2f}\n") self.result_text.insert(tk.END, f" • 最小值: {stats['min']:.2f}\n") self.result_text.insert(tk.END, f" • 25%分位: {stats['25%']:.2f}\n") self.result_text.insert(tk.END, f" • 中位数: {stats['50%']:.2f}\n") self.result_text.insert(tk.END, f" • 75%分位: {stats['75%']:.2f}\n") self.result_text.insert(tk.END, f" • 最大值: {stats['max']:.2f}\n") self.result_text.insert(tk.END, "\n") # 处理非数值列 if not non_numeric_cols.empty: self.result_text.insert(tk.END, "═══ 非数值型数据统计 ═══\n\n", 'header') for col in non_numeric_cols: value_counts = data[col].value_counts() unique_count = data[col].nunique() total_count = len(data[col]) self.result_text.insert(tk.END, f"▶ {col}\n", 'subtitle') self.result_text.insert(tk.END, f" • 总数据量: {total_count}\n") self.result_text.insert(tk.END, f" • 唯一值数量: {unique_count}\n") self.result_text.insert(tk.END, " • 前5项频率分布:\n") # 显示前5个值的频率分布 for val, count in value_counts.head().items(): percentage = (count / total_count) * 100 self.result_text.insert(tk.END, f" - {val}: {count} ({percentage:.1f}%)\n") self.result_text.insert(tk.END, "\n") def _run_correlation_analysis(self, data): """相关性分析""" numeric_data = data.select_dtypes(include=['int64', 'float64']) if numeric_data.empty: self.result_text.insert(tk.END, "⚠ 没有找到可以进相关性分析的数值型数据", 'warning') else: result = numeric_data.corr() self.result_text.insert(tk.END, "═══ 相关性分析结果 ═══\n\n", 'header') # 格式化相关性矩阵 for col1 in result.columns: self.result_text.insert(tk.END, f"▶ {col1} 的相关性:\n", 'subtitle') for col2 in result.columns: if col1 != col2: # 不显示自身的相关性 corr = result.loc[col1, col2] # 添加相关性强度的描述 strength = "" if abs(corr) > 0.7: strength = "强" elif abs(corr) > 0.4: strength = "中等" else: strength = "弱" self.result_text.insert(tk.END, f" • 与 {col2}: {corr:.3f} ({strength}相关)\n") self.result_text.insert(tk.END, "\n") def _run_distribution_analysis(self, data): """数据分布分析""" numeric_cols = data.select_dtypes(include=['int64', 'float64']).columns if numeric_cols.empty: self.result_text.insert(tk.END, "⚠ 没有找到可以分析的数值型数据", 'warning') return self.result_text.insert(tk.END, "═══ 数据分布分析 ═══\n\n", 'header') for col in numeric_cols: # 修改变量名,避免与参数名冲突 col_data = data[col].dropna() # 计算分布相关指标 skewness = col_data.skew() kurtosis = col_data.kurtosis() # 计算分位数 quantiles = col_data.quantile([0.1, 0.25, 0.5, 0.75, 0.9]) self.result_text.insert(tk.END, f"▶ {col}\n", 'subtitle') self.result_text.insert(tk.END, f" • 偏度: {skewness:.3f}\n") self.result_text.insert(tk.END, f" • 峰度: {kurtosis:.3f}\n") self.result_text.insert(tk.END, " • 分位数分布:\n") self.result_text.insert(tk.END, f" - 10%: {quantiles[0.1]:.2f}\n") self.result_text.insert(tk.END, f" - 25%: {quantiles[0.25]:.2f}\n") self.result_text.insert(tk.END, f" - 50%: {quantiles[0.5]:.2f}\n") self.result_text.insert(tk.END, f" - 75%: {quantiles[0.75]:.2f}\n") self.result_text.insert(tk.END, f" - 90%: {quantiles[0.9]:.2f}\n\n") def _run_time_series_analysis(self, data): """时间序列分析""" # 查找日期列 date_cols = data.select_dtypes(include=['datetime64']).columns if date_cols.empty: self.result_text.insert(tk.END, "⚠ 没有找到日期类型的列\n", 'warning') return self.result_text.insert(tk.END, "═══ 时间序列分析 ═══\n\n", 'header') for date_col in date_cols: self.result_text.insert(tk.END, f"▶ {date_col} 时间分布\n", 'subtitle') # 基本时间范围 time_min = data[date_col].min() time_max = data[date_col].max() time_range = time_max - time_min self.result_text.insert(tk.END, f" • 时间范围: {time_range.days} 天\n") self.result_text.insert(tk.END, f" • 起始时间: {time_min:%Y-%m-%d}\n") self.result_text.insert(tk.END, f" • 结束时间: {time_max:%Y-%m-%d}\n\n") # 按月份分布 monthly_counts = data[date_col].dt.month.value_counts().sort_index() self.result_text.insert(tk.END, " • 月份分布:\n") for month, count in monthly_counts.items(): self.result_text.insert(tk.END, f" - {month}月: {count}条记录\n") self.result_text.insert(tk.END, "\n") def _run_group_analysis(self, data): """分组统计分析""" # 获取可能的分组列(分类数据) category_cols = data.select_dtypes(include=['object', 'category']).columns numeric_cols = data.select_dtypes(include=['int64', 'float64']).columns if category_cols.empty or numeric_cols.empty: self.result_text.insert(tk.END, "⚠ 需要同时包含分类数据和数值数据\n", 'warning') return self.result_text.insert(tk.END, "═══ 分组统计分析 ═══\n\n", 'header') for cat_col in category_cols: self.result_text.insert(tk.END, f"▶ 按 {cat_col} 分组统计\n", 'subtitle') # 计算每个分组的基本统计量 for num_col in numeric_cols: group_stats = data.groupby(cat_col)[num_col].agg([ 'count', 'mean', 'std', 'min', 'max' ]) self.result_text.insert(tk.END, f" • {num_col} 统计:\n") for group_name, stats in group_stats.iterrows(): self.result_text.insert(tk.END, f" - {group_name}:\n") self.result_text.insert(tk.END, f" 数量: {stats['count']:.0f}\n") self.result_text.insert(tk.END, f" 均值: {stats['mean']:.2f}\n") self.result_text.insert(tk.END, f" 标准差: {stats['std']:.2f}\n") self.result_text.insert(tk.END, f" 最小值: {stats['min']:.2f}\n") self.result_text.insert(tk.END, f" 最大值: {stats['max']:.2f}\n") self.result_text.insert(tk.END, "\n") def _run_missing_value_analysis(self, data): """缺失值分析""" self.result_text.insert(tk.END, "═══ 缺失值分析 ═══\n\n", 'header') # 计算每列的缺失值 missing_stats = data.isnull().sum() total_rows = len(data) # 只显示有缺失值的列 missing_cols = missing_stats[missing_stats > 0] if missing_cols.empty: self.result_text.insert(tk.END, "✓ 数据中没有发现缺失值\n", 'subtitle') return self.result_text.insert(tk.END, "▶ 缺失值统计\n", 'subtitle') for col, missing_count in missing_cols.items(): missing_percentage = (missing_count / total_rows) * 100 self.result_text.insert(tk.END, f" • {col}:\n") self.result_text.insert(tk.END, f" - 缺失数量: {missing_count}\n") self.result_text.insert(tk.END, f" - 缺失比例: {missing_percentage:.2f}%\n") # 添加缺失值模式分析 self.result_text.insert(tk.END, "\n▶ 缺失值模式\n", 'subtitle') total_missing = data.isnull().sum().sum() self.result_text.insert(tk.END, f" • 总缺失值数量: {total_missing}\n") self.result_text.insert(tk.END, f" • 总缺失率: {(total_missing/(total_rows*len(data.columns))):.2f}%\n") def _run_cpk_analysis(self, data): """CPK分析""" numeric_cols = data.select_dtypes(include=['int64', 'float64']).columns if numeric_cols.empty: self.result_text.insert(tk.END, "⚠ 没有找到可以进CPK分析的数值型数据", 'warning') return # 创建输入对话框获取规格限 spec_dialog = tk.Toplevel(self.root) spec_dialog.title("输入规格限") spec_dialog.geometry("400x500") # 增加窗口大小 # 使对话框成为模态窗口 spec_dialog.transient(self.root) spec_dialog.grab_set() # 创建主框架,并添加滚动条 main_frame = ttk.Frame(spec_dialog) main_frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5) # 创建Canvas和滚动条 canvas = tk.Canvas(main_frame) scrollbar = ttk.Scrollbar(main_frame, orient="vertical", command=canvas.yview) # 创建内容框架 content_frame = ttk.Frame(canvas) # 配置Canvas canvas.configure(yscrollcommand=scrollbar.set) # 创建规格限输入框 specs = {} row = 0 # 添加标题标签 title_label = ttk.Label(content_frame, text="请输入各列的规格上下限:", font=('Arial', 10, 'bold')) title_label.grid(row=row, column=0, columnspan=3, pady=10, padx=5, sticky='w') row += 1 for col in numeric_cols: # 列名标签 col_label = ttk.Label(content_frame, text=f"{col}:", font=('Arial', 9)) col_label.grid(row=row, column=0, pady=5, padx=5, sticky='w') # USL输入框和标签 usl_frame = ttk.Frame(content_frame) usl_frame.grid(row=row, column=1, padx=5, sticky='w') usl_var = tk.StringVar() ttk.Entry(usl_frame, textvariable=usl_var, width=12).pack(side=tk.LEFT, padx=2) ttk.Label(usl_frame, text="USL").pack(side=tk.LEFT, padx=2) row += 1 # LSL输入框和标签 lsl_frame = ttk.Frame(content_frame) lsl_frame.grid(row=row, column=1, padx=5, sticky='w') lsl_var = tk.StringVar() ttk.Entry(lsl_frame, textvariable=lsl_var, width=12).pack(side=tk.LEFT, padx=2) ttk.Label(lsl_frame, text="LSL").pack(side=tk.LEFT, padx=2) specs[col] = {'usl': usl_var, 'lsl': lsl_var} row += 1 # 添加分隔线 ttk.Separator(content_frame, orient='horizontal').grid( row=row, column=0, columnspan=3, sticky='ew', pady=5) row += 1 # 添加按钮框架 button_frame = ttk.Frame(content_frame) button_frame.grid(row=row, column=0, columnspan=3, pady=10) ttk.Button(button_frame, text="计算CPK", command=lambda: calculate_cpk()).pack(side=tk.LEFT, padx=5) ttk.Button(button_frame, text="取消", command=spec_dialog.destroy).pack(side=tk.LEFT, padx=5) # 放置Canvas和滚动条 canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # 将content_frame放入canvas canvas_window = canvas.create_window((0, 0), window=content_frame, anchor='nw') # 配置canvas滚动区域 def configure_scroll_region(event): canvas.configure(scrollregion=canvas.bbox('all')) # 配置canvas宽度 def configure_canvas_width(event): canvas.itemconfig(canvas_window, width=event.width) # 绑定事件 content_frame.bind('<Configure>', configure_scroll_region) canvas.bind('<Configure>', configure_canvas_width) # 绑定鼠标滚轮 def on_mousewheel(event): canvas.yview_scroll(int(-1 * (event.delta / 120)), "units") canvas.bind_all("<MouseWheel>", on_mousewheel) def calculate_cpk(): """计算CPK""" try: # 确保文本框可编辑 self.result_text.config(state='normal') self.result_text.delete(1.0, tk.END) for col in numeric_cols: try: # 获取规格限 usl = float(specs[col]['usl'].get()) lsl = float(specs[col]['lsl'].get()) # 获取数据 values = data[col].dropna() # 计算统计量 mean = values.mean() std = values.std() # 计算CPU和CPL cpu = (usl - mean) / (3 * std) cpl = (mean - lsl) / (3 * std) # 计算CPK cpk = min(cpu, cpl) # 计算过程能力评级 rating = "未知" if cpk >= 1.67: rating = "极佳" elif cpk >= 1.33: rating = "良好" elif cpk >= 1.0: rating = "合格" else: rating = "不合格" # 显示结果 self.result_text.insert(tk.END, f"▶ {col}\n", 'subtitle') self.result_text.insert(tk.END, f" • 均值: {mean:.3f}\n") self.result_text.insert(tk.END, f" • 标准差: {std:.3f}\n") self.result_text.insert(tk.END, f" • USL: {usl:.3f}\n") self.result_text.insert(tk.END, f" • LSL: {lsl:.3f}\n") self.result_text.insert(tk.END, f" • CPU: {cpu:.3f}\n") self.result_text.insert(tk.END, f" • CPL: {cpl:.3f}\n") self.result_text.insert(tk.END, f" • CPK: {cpk:.3f}\n") self.result_text.insert(tk.END, f" • 过程能力评级: {rating}\n\n") except ValueError: self.result_text.insert(tk.END, f"⚠ {col}: 输入数值无效\n", 'warning') except Exception as e: self.result_text.insert(tk.END, f"⚠ {col}: 计算出错 - {str(e)}\n", 'error') # 设置文本框为只读 self.result_text.config(state='disabled') # 关闭对话框 spec_dialog.destroy() except Exception as e: messagebox.showerror("错误", f"计算过程出错:{str(e)}") # 确保发生错误时也设置文本框为只读 self.result_text.config(state='disabled') def open_file(self): file_path = filedialog.askopenfilename( title="选择文件", filetypes=(("Excel files", "*.xlsx;*.xls"), ("All files", "*.*")) ) if file_path: try: # 使用pandas读取Excel数据 self.current_data = pd.read_excel(file_path) data = self.current_data # 清除现有的Treeview数据 self.tree.delete(*self.tree.get_children()) # 设置Treeview的列和标题 self.tree["columns"] = list(data.columns) for col in data.columns: # 更精确的列宽计算 max_width = max( len(str(col)) * 7, # 进一步减小系数 data[col].astype(str).str.len().max() * 7 ) width = min(max(max_width, 50), 150) # 更紧凑的列宽范围 self.tree.column(col, anchor=tk.W, width=width, minwidth=40, # 更小的最小宽度 stretch=True ) self.tree.heading(col, text=col, anchor=tk.W, ) # 插入数据到Treeview for i, (index, row) in enumerate(data.iterrows()): tags = ('evenrow',) if i % 2 == 0 else ('oddrow',) self.tree.insert("", "end", values=list(row), tags=tags) # 配置更细微的交替颜色 self.tree.tag_configure('oddrow', background='#FAFAFA') # 更浅的灰色 self.tree.tag_configure('evenrow', background='#FFFFFF') # 纯白色 # 更新状态栏 self.status_bar.config( text=f"已加载 {len(data)} 数据,{len(data.columns)} 列 | {file_path}" ) # 清除之前的分析结果 self.result_text.config(state='normal') self.result_text.delete(1.0, tk.END) self.result_text.config(state='disabled') self.algorithm_var.set("请选择分析方法") except Exception as e: messagebox.showerror("错误", f"无法读取文件: {e}") self.status_bar.config(text="读取文件失败") def load_config(self): """加载配置文件""" config_dir = os.path.expanduser("~/.data_analysis_app") self.config_file = os.path.join(config_dir, "config.json") # 确保配置目录存在 if not os.path.exists(config_dir): os.makedirs(config_dir) if os.path.exists(self.config_file): try: with open(self.config_file, 'r', encoding='utf-8') as f: return json.load(f) except: return {} return {} def save_config(self): """保存配置到文件""" config = { "window_width": self.root.winfo_width(), "window_height": self.root.winfo_height(), "paned_position": self.paned_window.sashpos(0) } try: with open(self.config_file, 'w', encoding='utf-8') as f: json.dump(config, f, indent=4) except Exception as e: print(f"保存配置失败: {e}") def on_sash_moved(self, event): """分隔条移动后的处理""" self.save_config() def on_closing(self): """窗口关闭时的处理""" self.save_config() self.root.destroy() def open_formula_window(self): """打开公式编辑窗口""" formula_window = tk.Toplevel(self.root) formula_window.title("自定义公式") formula_window.geometry("600x400") # 使窗口居中 window_width = 600 window_height = 400 screen_width = formula_window.winfo_screenwidth() screen_height = formula_window.winfo_screenheight() x = int((screen_width - window_width) / 2) y = int((screen_height - window_height) / 2) formula_window.geometry(f"{window_width}x{window_height}+{x}+{y}") # 创建主框架 main_frame = ttk.Frame(formula_window, padding="10") main_frame.pack(fill=tk.BOTH, expand=True) # 创建说明标签 ttk.Label(main_frame, text="在这里输入您的自定义公式:", font=('Arial', 10)).pack(anchor=tk.W) # 创建公式名称输入框 name_frame = ttk.Frame(main_frame) name_frame.pack(fill=tk.X, pady=(10,5)) ttk.Label(name_frame, text="公式名称:").pack(side=tk.LEFT) formula_name = ttk.Entry(name_frame) formula_name.pack(side=tk.LEFT, fill=tk.X, expand=True) # 创建公式输入区域 formula_frame = ttk.LabelFrame(main_frame, text="公式内容", padding="5") formula_frame.pack(fill=tk.BOTH, expand=True, pady=(5,10)) # 创建文本编辑器和滚动条的容器 text_container = ttk.Frame(formula_frame) text_container.pack(fill=tk.BOTH, expand=True) # 创建文本编辑器 formula_text = tk.Text(text_container, wrap=tk.WORD, font=('Consolas', 11)) # 创建垂直滚动条 v_scrollbar = ttk.Scrollbar(text_container, orient=tk.VERTICAL, command=formula_text.yview) v_scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # 创建水平滚动条 h_scrollbar = ttk.Scrollbar(formula_frame, orient=tk.HORIZONTAL, command=formula_text.xview) h_scrollbar.pack(side=tk.BOTTOM, fill=tk.X) # 配置文本框的滚动 formula_text.configure(yscrollcommand=v_scrollbar.set, xscrollcommand=h_scrollbar.set) formula_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 添加示例文本 example_text = """# 示例公式: # 可以使用 Python 语法编写公式 # 数据可通过 data 变量访问 def calculate(data): # 示例:计算某列的平均值 result = data['列名'].mean() return result # 更多示例: # 1. 计算多列的平均值 # result = data[['列1', '列2', '列3']].mean() # 2. 条件筛选 # result = data[data['列名'] > 100].mean() # 3. 自定义计算 # result = (data['列1'] + data['列2']) / 2 # 4. 分组统计 # result = data.groupby('分组列')['值列'].mean() # 5. 数据转换 # result = data['列名'].apply(lambda x: x * 2) """ formula_text.insert('1.0', example_text) # 创建按钮框架 button_frame = ttk.Frame(main_frame) button_frame.pack(fill=tk.X, pady=(0,5)) def save_formula(): """保存公式""" name = formula_name.get().strip() formula = formula_text.get('1.0', tk.END).strip() if not name: messagebox.showwarning("警告", "请输入公式名称") return if not formula: messagebox.showwarning("警告", "请输入公式内容") return try: # 保存公式到文件 formulas_file = "custom_formulas.json" formulas = {} # 读取现有公式 if os.path.exists(formulas_file): with open(formulas_file, 'r', encoding='utf-8') as f: formulas = json.load(f) # 添加或更新公式 formulas[name] = formula # 保存到文件 with open(formulas_file, 'w', encoding='utf-8') as f: json.dump(formulas, f, indent=4, ensure_ascii=False) messagebox.showinfo("成功", "公式保存成功!") formula_window.destroy() except Exception as e: messagebox.showerror("错误", f"保存公式失败:{str(e)}") def test_formula(): """测试公式""" if self.current_data is None: messagebox.showwarning("警告", "请先加载数据") return formula = formula_text.get('1.0', tk.END).strip() if not formula: messagebox.showwarning("警告", "请输入公式内容") return try: # 创建一个本地命名空间 local_dict = {} # 执公式代码 exec(formula, globals(), local_dict) if 'calculate' not in local_dict: raise ValueError("未找到 calculate 函数") # 执计算 result = local_dict['calculate'](self.current_data) # 显示结果 messagebox.showinfo("测试结果", f"计算结果:{result}") except Exception as e: messagebox.showerror("错误", f"公式测试失败:{str(e)}") # 添加按钮 ttk.Button(button_frame, text="测试公式", command=test_formula).pack(side=tk.LEFT, padx=5) ttk.Button(button_frame, text="保存公式", command=save_formula).pack(side=tk.LEFT, padx=5) ttk.Button(button_frame, text="取消", command=formula_window.destroy).pack(side=tk.RIGHT, padx=5) def get_resource_path(self, relative_path): """获取资源文件的绝对路径""" try: # PyInstaller创建临时文件夹,将路径存储在_MEIPASS中 base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) if __name__ == "__main__": root = tk.Tk() app = DataAnalysisApp(root) root.mainloop()
06-17
import pandas as pd import tkinter as tk from tkinter import ttk, messagebox, filedialog import os import json import openpyxl from openpyxl.utils.dataframe import dataframe_to_rows from tkinter.font import Font import traceback import logging import sys import io import numpy as np from PIL import Image, ImageTk # 配置日志系统 logging.basicConfig( filename='app.log', level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s' ) logger = logging.getLogger() class ScrollableFrame(ttk.Frame): """自定义可滚动框架实现""" def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) # 创建Canvas和滚动条 self.canvas = tk.Canvas(self, highlightthickness=0) self.scrollbar = ttk.Scrollbar(self, orient="vertical", command=self.canvas.yview) self.scrollable_frame = ttk.Frame(self.canvas) # 配置Canvas self.canvas.configure(yscrollcommand=self.scrollbar.set) self.canvas_frame = self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw") # 布局 self.canvas.pack(side="left", fill="both", expand=True, padx=0, pady=0) self.scrollbar.pack(side="right", fill="y") # 绑定事件 self.scrollable_frame.bind("<Configure>", self.on_frame_configure) self.canvas.bind("<Configure>", self.on_canvas_configure) self.canvas.bind_all("<MouseWheel>", self.on_mousewheel) def on_frame_configure(self, event): """当内部框架大小改变时更新滚动区域""" self.canvas.configure(scrollregion=self.canvas.bbox("all")) def on_canvas_configure(self, event): """当Canvas大小改变时调整内部框架宽度""" self.canvas.itemconfig(self.canvas_frame, width=event.width) def on_mousewheel(self, event): """鼠标滚轮滚动支持""" self.canvas.yview_scroll(int(-1*(event.delta/120)), "units") class ToolTip: """自定义工具提示类""" def __init__(self, widget, text='', max_width=500): self.widget = widget self.text = text self.max_width = max_width self.tip_window = None self.id = None self.x = self.y = 0 def show_tip(self, text, x, y): """显示工具提示""" self.text = text if self.tip_window or not self.text: return # 计算屏幕位置 x = x + self.widget.winfo_rootx() + 20 y = y + self.widget.winfo_rooty() + 20 # 创建顶层窗口 self.tip_window = tk.Toplevel(self.widget) self.tip_window.wm_overrideredirect(True) self.tip_window.wm_geometry(f"+{x}+{y}") # 创建标签 label = tk.Label(self.tip_window, text=self.text, justify='left', background="#ffffe0", relief='solid', borderwidth=1, wraplength=self.max_width) label.pack(ipadx=1, ipady=1) def hide_tip(self): """隐藏工具提示""" if self.tip_window: self.tip_window.destroy() self.tip_window = None class ExcelControlPanel: def __init__(self, master): self.master = master master.title("功能点确认系统") master.geometry("1280x800") master.configure(bg="#f0f2f5") # 设置全局样式 self.set_styles() # 加载配置 self.config = self.load_config() # 初始化多列确认配置 self.confirmation_columns = self.config.get("confirmation_columns", []) self.column_order = self.config.get("column_order", []) # 创建界面元素 self.create_widgets() # 初始化数据 self.excel_path = "" self.df = None self.check_states = {} # 存储每个功能点的复选框状态 self.current_sheet = "" self.header_row = 0 self.full_descriptions = {} # 存储完整的功能点描述 self.tooltip = None # 工具提示实例 # 更新列选择器 self.update_col_selector() def set_styles(self): """设置全局样式和字体""" style = ttk.Style() style.theme_use('clam') # 自定义字体 self.title_font = Font(family="Microsoft YaHei", size=16, weight="bold") self.subtitle_font = Font(family="Microsoft YaHei", size=10) self.normal_font = Font(family="Microsoft YaHei", size=9) # 配置样式 style.configure("TFrame", background="#f0f2f5") style.configure("TLabel", font=self.normal_font, background="#f0f2f5", foreground="#333") style.configure("TButton", font=self.normal_font, padding=8) # Treeview样式 - 增加以容纳更多内容 style.configure("Treeview.Heading", font=self.subtitle_font, background="#4a76b5", foreground="white") style.configure("Treeview", font=self.normal_font, rowheight=50, background="white", fieldbackground="white") # 添加交替背景色 style.configure("evenrow.Treeview", background="#f8f9fa") style.configure("oddrow.Treeview", background="white") # 状态栏样式 style.configure("Status.TFrame", background="#e0e0e0") style.configure("Status.TLabel", font=self.normal_font, background="#4a76b5", foreground="#ffffff", padding=5) # 卡片样式 style.configure("Card.TFrame", background="white", borderwidth=0, relief="solid", padding=10, bordercolor="#e1e4e8") style.configure("Card.TLabelframe", background="white", borderwidth=1, relief="solid", padding=10, bordercolor="#e1e4e8") style.configure("Card.TLabelframe.Label", font=self.subtitle_font, foreground="#2c3e50", background="white") # 按钮样式 style.map("Primary.TButton", background=[("active", "#3a66a5"), ("pressed", "#2a5685")], foreground=[("active", "white"), ("pressed", "white")]) style.configure("Primary.TButton", background="#4a76b5", foreground="white", font=self.subtitle_font, borderwidth=0) style.map("Success.TButton", background=[("active", "#28a745"), ("pressed", "#218838")], foreground=[("active", "white"), ("pressed", "white")]) style.configure("Success.TButton", background="#28a745", foreground="white", font=self.subtitle_font, borderwidth=0) style.map("Danger.TButton", background=[("active", "#dc3545"), ("pressed", "#c82333")], foreground=[("active", "white"), ("pressed", "white")]) style.configure("Danger.TButton", background="#dc3545", foreground="white", font=self.subtitle_font, borderwidth=0) # 输入框样式 style.configure("Custom.TEntry", fieldbackground="#f8f9fa", bordercolor="#ced4da") def load_config(self): """加载配置文件""" config_path = "excel_config.json" default_config = { "id_col": "No.", "desc_col": "レビュー観点(CHN)", "status_col": "レビュー結果", "sheet_name": "", "header_row": 9, "last_dir": os.getcwd(), "custom_status": "OK", "confirmation_columns": [], "column_order": ["selection", "id", "description", "status"], "max_desc_length": 300 # 新增:最大描述长度 } if os.path.exists(config_path): try: with open(config_path, 'r', encoding='utf-8') as f: config = json.load(f) # 确保所有键都存在 for key in default_config: if key not in config: config[key] = default_config[key] return config except Exception as e: logger.error(f"加载配置文件失败: {str(e)}") return default_config return default_config def save_config(self): """保存配置文件""" config_path = "excel_config.json" try: with open(config_path, 'w', encoding='utf-8') as f: json.dump(self.config, f, ensure_ascii=False, indent=2) except Exception as e: logger.error(f"保存配置文件失败: {str(e)}") def create_widgets(self): """创建现代化界面元素""" # 主容器 main_container = ttk.Frame(self.master, style="Card.TFrame") main_container.pack(fill="both", expand=True, padx=20, pady=20) # 标题栏 title_frame = ttk.Frame(main_container, style="Title.TFrame") title_frame.pack(fill="x", pady=(0, 20)) ttk.Label(title_frame, text="功能点确认系统", font=self.title_font, background="#4a76b5", foreground="white", padding=10).pack(side="left", fill="x", expand=True) # 主内容区域 content_frame = ttk.Frame(main_container) content_frame.pack(fill="both", expand=True) # 左侧控制面板 - 可滚动 control_container = ttk.Frame(content_frame, width=350) control_container.pack(side="left", fill="y", padx=(0, 20)) # 创建自定义可滚动框架 scrollable_frame = ScrollableFrame(control_container, width=350) scrollable_frame.pack(fill="both", expand=True) # 获取内部框架 inner_frame = scrollable_frame.scrollable_frame # 美化控制面板 control_card = ttk.LabelFrame(inner_frame, text="控制面板", style="Card.TLabelframe") control_card.pack(fill="both", expand=True, padx=10, pady=10) # 文件选择区域 file_frame = ttk.LabelFrame(control_card, text="Excel文件设置") file_frame.pack(fill="x", padx=10, pady=(0, 15)) ttk.Label(file_frame, text="Excel文件路径:").pack(anchor="w", pady=(0, 5), padx=10) path_frame = ttk.Frame(file_frame) path_frame.pack(fill="x", pady=5, padx=10) self.path_entry = ttk.Entry(path_frame, width=30, style="Custom.TEntry") self.path_entry.pack(side="left", fill="x", expand=True, padx=(0, 5)) ttk.Button(path_frame, text="浏览", command=self.browse_file, width=8).pack(side="left") ttk.Button(file_frame, text="加载数据", command=self.load_data, style="Primary.TButton").pack(fill="x", pady=10, padx=10) # Sheet选择区域 sheet_frame = ttk.LabelFrame(control_card, text="工作表设置") sheet_frame.pack(fill="x", padx=10, pady=(0, 15)) ttk.Label(sheet_frame, text="当前Sheet:").pack(anchor="w", padx=10, pady=(10, 0)) self.sheet_var = tk.StringVar(value=self.config.get("sheet_name", "未选择")) sheet_display = ttk.Label(sheet_frame, textvariable=self.sheet_var, font=self.subtitle_font) sheet_display.pack(anchor="w", pady=(0, 10), padx=10) ttk.Button(sheet_frame, text="选择工作表", command=self.select_sheet, style="Primary.TButton").pack(fill="x", padx=10, pady=(0, 10)) # 表头设置 header_frame = ttk.LabelFrame(control_card, text="表头设置") header_frame.pack(fill="x", padx=10, pady=(0, 15)) ttk.Label(header_frame, text="表头所在:").pack(anchor="w", padx=10, pady=(10, 0)) self.header_var = tk.IntVar(value=self.config.get("header_row", 9)) ttk.Entry(header_frame, textvariable=self.header_var, width=10, style="Custom.TEntry").pack(anchor="w", pady=5, padx=10) # 列名配置区域 col_frame = ttk.LabelFrame(control_card, text="列名配置") col_frame.pack(fill="x", padx=10, pady=(0, 15)) # ID列 ttk.Label(col_frame, text="ID列名:").pack(anchor="w", padx=10, pady=(10, 0)) self.id_col_var = tk.StringVar(value=self.config["id_col"]) ttk.Entry(col_frame, textvariable=self.id_col_var, style="Custom.TEntry").pack(fill="x", pady=(0, 10), padx=10) # 功能点列 ttk.Label(col_frame, text="功能点列名:").pack(anchor="w", padx=10, pady=(0, 0)) self.desc_col_var = tk.StringVar(value=self.config["desc_col"]) ttk.Entry(col_frame, textvariable=self.desc_col_var, style="Custom.TEntry").pack(fill="x", pady=(0, 10), padx=10) # 状态列 ttk.Label(col_frame, text="状态列名:").pack(anchor="w", padx=10, pady=(0, 0)) self.status_col_var = tk.StringVar(value=self.config["status_col"]) ttk.Entry(col_frame, textvariable=self.status_col_var, style="Custom.TEntry").pack(fill="x", pady=(0, 10), padx=10) # 自定义状态 ttk.Label(col_frame, text="自定义确认状态:").pack(anchor="w", padx=10, pady=(0, 0)) self.custom_status_var = tk.StringVar(value=self.config.get("custom_status", "OK")) ttk.Entry(col_frame, textvariable=self.custom_status_var, style="Custom.TEntry").pack(fill="x", pady=(0, 10), padx=10) # 描述长度设置 ttk.Label(col_frame, text="最大描述长度:").pack(anchor="w", padx=10, pady=(0, 0)) self.max_desc_var = tk.IntVar(value=self.config.get("max_desc_length", 300)) ttk.Entry(col_frame, textvariable=self.max_desc_var, style="Custom.TEntry").pack(fill="x", pady=(0, 10), padx=10) # 多列确认设置 multi_col_frame = ttk.LabelFrame(control_card, text="多列确认设置") multi_col_frame.pack(fill="x", padx=10, pady=(0, 15)) # 列选择器 ttk.Label(multi_col_frame, text="选择要确认的列:").pack(anchor="w", padx=10, pady=(10, 0)) col_selector_frame = ttk.Frame(multi_col_frame) col_selector_frame.pack(fill="x", pady=5, padx=10) self.col_selector = ttk.Combobox(col_selector_frame, state="readonly", width=15) self.col_selector.pack(side="left", fill="x", expand=True, padx=(0, 5)) # 添加/移除按钮 ttk.Button(col_selector_frame, text="添加", command=self.add_confirmation_column, width=8).pack(side="left") # 已选列列表 ttk.Label(multi_col_frame, text="已选确认列:").pack(anchor="w", padx=10, pady=(10, 0)) self.selected_cols_listbox = tk.Listbox(multi_col_frame, height=3, font=self.normal_font, bg="#f8f9fa", highlightthickness=0) self.selected_cols_listbox.pack(fill="x", pady=5, padx=10) # 加载已配置的确认列 for col in self.confirmation_columns: self.selected_cols_listbox.insert(tk.END, col) # 移除按钮 remove_btn = ttk.Button(multi_col_frame, text="移除选中列", command=self.remove_confirmation_column) remove_btn.pack(fill="x", pady=(0, 10), padx=10) # 操作按钮区域 btn_frame = ttk.Frame(control_card) btn_frame.pack(fill="x", padx=5, pady=10) ttk.Button(btn_frame, text="保存配置", command=self.save_current_config, style="Success.TButton").pack(fill="x", pady=5) ttk.Button(btn_frame, text="确认选中项", command=self.confirm_selected, style="Primary.TButton").pack(fill="x", pady=5) ttk.Button(btn_frame, text="全选", command=self.select_all, style="Primary.TButton").pack(side="left", fill="x", expand=True, pady=5) ttk.Button(btn_frame, text="取消全选", command=self.deselect_all, style="Danger.TButton").pack(side="left", fill="x", expand=True, pady=5) ttk.Button(btn_frame, text="保存到Excel", command=self.save_to_excel, style="Success.TButton").pack(fill="x", pady=5) # 数据显示区域 data_card = ttk.LabelFrame(content_frame, text="功能点列表", style="Card.TLabelframe") data_card.pack(side="right", fill="both", expand=True) # Treeview容器 tree_frame = ttk.Frame(data_card) tree_frame.pack(fill="both", expand=True, padx=5, pady=5) # 创建Treeview self.tree = ttk.Treeview(tree_frame, columns=[], show="headings", height=20) # 滚动条设置 vsb = ttk.Scrollbar(tree_frame, orient="vertical", command=self.tree.yview) hsb = ttk.Scrollbar(tree_frame, orient="horizontal", command=self.tree.xview) self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set) # 使用grid布局管理器 self.tree.grid(row=0, column=0, sticky="nsew") vsb.grid(row=0, column=1, sticky="ns") hsb.grid(row=1, column=0, sticky="ew") # 配置网格权重 tree_frame.columnconfigure(0, weight=1) tree_frame.rowconfigure(0, weight=1) # 添加标签样式 - 使用交替背景色 self.tree.tag_configure("evenrow", background="#f8f9fa") self.tree.tag_configure("oddrow", background="white") self.tree.tag_configure("confirmed", background="#d4edda") self.tree.tag_configure("pending", background="#f8d7da") # 绑定列调整事件 self.tree.bind("<Configure>", self.on_tree_configure) # 绑定点击事件处理复选框 self.tree.bind("<Button-1>", self.on_tree_click) # 绑定选择事件 self.tree.bind("<<TreeviewSelect>>", self.on_tree_select) # 绑定鼠标移动事件用于显示工具提示 self.tree.bind("<Motion>", self.on_tree_motion) self.tree.bind("<Leave>", self.on_tree_leave) # 详情区域 detail_frame = ttk.LabelFrame(data_card, text="功能点详情", style="Card.TLabelframe") detail_frame.pack(fill="x", padx=5, pady=(0, 5)) self.detail_text = tk.Text(detail_frame, wrap="word", font=self.normal_font, height=3, bg="#f8f9fa", relief="solid", borderwidth=1) scroll_detail = ttk.Scrollbar(detail_frame, command=self.detail_text.yview) self.detail_text.config(yscrollcommand=scroll_detail.set) # 使用grid布局文本框和滚动条 self.detail_text.grid(row=0, column=0, sticky="nsew", padx=5, pady=5) scroll_detail.grid(row=0, column=1, sticky="ns", pady=5) # 配置网格权重 detail_frame.columnconfigure(0, weight=1) detail_frame.rowconfigure(0, weight=1) self.detail_text.config(state="disabled") # 状态栏 self.status_var = tk.StringVar() self.status_var.set("就绪 - 请选择Excel文件开始") # 创建状态栏容器框架 status_container = ttk.Frame(self.master, style="Status.TFrame") status_container.pack(side="bottom", fill="x", padx=0, pady=0) # 状态标签 status_bar = ttk.Label( status_container, textvariable=self.status_var, style="Status.TLabel", anchor="w", padding=(10, 5, 10, 5) ) status_bar.pack(side="bottom", fill="x", expand=False) # 初始化Treeview列 self.update_treeview_columns() # 创建工具提示 self.tooltip = ToolTip(self.tree) def update_treeview_columns(self): """更新Treeview列""" # 基础列 base_columns = ["selection", "id", "description", "status"] # 所有列 = 基础列 + 确认列 all_columns = base_columns + self.confirmation_columns # 如果列顺序未设置或长度不匹配,则重置 if not self.column_order or len(self.column_order) != len(all_columns): self.column_order = all_columns # 重新配置Treeview self.tree.configure(columns=self.column_order) # 设置列标题 column_titles = { "selection": "选择", "id": "ID", "description": "功能点", "status": "状态" } for col in self.column_order: if col in column_titles: self.tree.heading(col, text=column_titles[col]) else: self.tree.heading(col, text=col) # 设置列属性 if col == "selection": self.tree.column(col, width=50, anchor="center") elif col == "id": self.tree.column(col, width=100, anchor="center") elif col == "description": self.tree.column(col, width=400, anchor="w", stretch=True) elif col == "status": self.tree.column(col, width=100, anchor="center") else: # 确认列 self.tree.column(col, width=100, anchor="center") # 调整列宽 self.adjust_columns() def on_tree_configure(self, event): """Treeview大小改变时调整列宽""" self.adjust_columns() def on_tree_select(self, event): """当Treeview中选中时,显示功能点详情""" selected_items = self.tree.selection() if not selected_items: return item = selected_items[0] # 获取完整功能点描述 full_desc = self.full_descriptions.get(item, "无描述") self.detail_text.config(state="normal") self.detail_text.delete(1.0, tk.END) self.detail_text.insert(tk.END, full_desc) self.detail_text.config(state="disabled") def on_tree_motion(self, event): """当鼠标在Treeview上移动时,显示工具提示""" region = self.tree.identify("region", event.x, event.y) if region == "cell": column = self.tree.identify_column(event.x) item = self.tree.identify_row(event.y) if not item: return # 获取列索引 col_idx = int(column[1:]) - 1 columns = self.tree["columns"] # 确保索引有效 if col_idx >= len(columns): return # 获取列标识符 col_id = columns[col_idx] # 只处理"功能点"列 if self.tree.heading(col_id, "text") == "功能点": # 获取完整描述 full_desc = self.full_descriptions.get(item, "") # 如果描述很长,显示工具提示 if full_desc and len(full_desc) > self.config.get("max_desc_length", 300): self.tooltip.show_tip(full_desc, event.x, event.y) def on_tree_leave(self, event): """当鼠标离开Treeview时,隐藏工具提示""" self.tooltip.hide_tip() def on_tree_click(self, event): """处理Treeview点击事件,切换复选框状态""" region = self.tree.identify("region", event.x, event.y) if region == "cell": column = self.tree.identify_column(event.x) item = self.tree.identify_row(event.y) if not item: return # 获取列索引 col_idx = int(column[1:]) - 1 columns = self.tree["columns"] # 确保索引有效 if col_idx >= len(columns): return # 获取列标识符 col_id = columns[col_idx] # 只处理"选择"列 if self.tree.heading(col_id, "text") == "选择": # 获取当前状态 values = list(self.tree.item(item, "values")) current_state = values[col_idx] # 切换状态 if current_state == "☐": new_state = "☑" else: new_state = "☐" # 更新Treeview values[col_idx] = new_state self.tree.item(item, values=values) # 更新状态存储 self.check_states[item] = (new_state == "☑") # 更新的整体状态显示 self.update_row_status(item) def update_row_status(self, item_id): """根据复选框状态更新状态显示""" values = list(self.tree.item(item_id, "values")) is_checked = self.check_states.get(item_id, False) # 找到状态列的位置 status_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "状态": status_col_idx = idx break if status_col_idx is not None and status_col_idx < len(values): if is_checked: values[status_col_idx] = "✓ 待确认" else: values[status_col_idx] = "✗ 未确认" self.tree.item(item_id, values=values) def adjust_columns(self, event=None): """根据窗口大小自动调整列宽""" if not self.tree.winfo_exists(): return width = self.tree.winfo_width() if width < 100: # 防止宽度过小 return # 计算可用宽度 available_width = width - 20 # 减去滚动条宽度 # 设置列宽比例 - 增加功能点列的比例 column_weights = { "selection": 0.05, "id": 0.10, "description": 0.65, "status": 0.08 } # 设置基础列宽 for col, weight in column_weights.items(): if col in self.tree["columns"]: col_width = int(available_width * weight) self.tree.column(col, width=col_width) # 设置确认列宽 if self.confirmation_columns: confirm_col_width = int(available_width * 0.06) # 减小确认列宽度 for col in self.confirmation_columns: if col in self.tree["columns"]: self.tree.column(col, width=confirm_col_width) def update_col_selector(self): """更新列选择器""" if self.df is not None: self.col_selector["values"] = list(self.df.columns) def add_confirmation_column(self): """添加确认列""" col = self.col_selector.get() if col and col not in self.confirmation_columns: self.confirmation_columns.append(col) self.selected_cols_listbox.insert(tk.END, col) self.update_treeview_columns() def remove_confirmation_column(self): """移除确认列""" selection = self.selected_cols_listbox.curselection() if selection: index = selection[0] self.confirmation_columns.pop(index) self.selected_cols_listbox.delete(index) self.update_treeview_columns() def browse_file(self): initial_dir = self.config.get("last_dir", os.getcwd()) file_path = filedialog.askopenfilename( initialdir=initial_dir, filetypes=[("Excel文件", "*.xlsx;*.xls")] ) if file_path: self.path_entry.delete(0, tk.END) self.path_entry.insert(0, file_path) # 更新最后访问目录 self.config["last_dir"] = os.path.dirname(file_path) self.save_config() def select_sheet(self): """选择工作表""" file_path = self.path_entry.get() if not file_path or not os.path.exists(file_path): messagebox.showerror("错误", "请先选择有效的Excel文件") return try: # 获取所有sheet名称 xl = pd.ExcelFile(file_path) sheet_names = xl.sheet_names # 创建选择对话框 sheet_dialog = tk.Toplevel(self.master) sheet_dialog.title("选择工作表") sheet_dialog.geometry("400x300") sheet_dialog.transient(self.master) sheet_dialog.grab_set() sheet_dialog.configure(bg="#f5f7fa") ttk.Label(sheet_dialog, text="请选择工作表:", font=self.subtitle_font, background="#f5f7fa").pack(pady=10) # 使用Treeview显示工作表 sheet_tree = ttk.Treeview(sheet_dialog, columns=("名称",), show="headings", height=8) sheet_tree.heading("名称", text="工作表名称") sheet_tree.column("名称", width=350) sheet_tree.pack(fill="both", expand=True, padx=20, pady=5) for name in sheet_names: sheet_tree.insert("", "end", values=(name,)) # 按钮框架 btn_frame = ttk.Frame(sheet_dialog) btn_frame.pack(fill="x", padx=20, pady=10) def on_select(): selected = sheet_tree.selection() if selected: self.current_sheet = sheet_tree.item(selected[0], "values")[0] self.sheet_var.set(self.current_sheet) # 保存工作表名称到配置 self.config["sheet_name"] = self.current_sheet self.save_config() sheet_dialog.destroy() ttk.Button(btn_frame, text="取消", command=sheet_dialog.destroy).pack(side="right", padx=5) ttk.Button(btn_frame, text="确定", command=on_select, style="Primary.TButton").pack(side="right") except Exception as e: messagebox.showerror("错误", f"读取Excel失败: {str(e)}") logger.error(f"选择工作表失败: {str(e)}") def load_data(self): file_path = self.path_entry.get() if not file_path or not os.path.exists(file_path): messagebox.showerror("错误", "无效的文件路径") return # 在状态栏显示加载中 self.status_var.set("正在加载数据...") self.master.update() # 强制更新界面 # 清空Treeview for item in self.tree.get_children(): self.tree.delete(item) self.check_states = {} self.full_descriptions = {} # 获取当前配置 id_col = self.id_col_var.get().strip() desc_col = self.desc_col_var.get().strip() status_col = self.status_col_var.get().strip() sheet_name = self.sheet_var.get() or None header_row = self.header_var.get() - 1 # pandas header是0-based索引 max_desc_length = self.max_desc_var.get() # 最大描述长度 # 直接调用加载任务 self.load_task(file_path, id_col, desc_col, status_col, sheet_name, header_row, max_desc_length) def load_task(self, file_path, id_col, desc_col, status_col, sheet_name, header_row, max_desc_length): try: # 确保使用正确的sheet_name if not sheet_name and self.config.get("sheet_name"): sheet_name = self.config["sheet_name"] # 从配置获取工作表名 # 读取Excel文件 if sheet_name: self.df = pd.read_excel( file_path, sheet_name=sheet_name, header=header_row ) else: # 如果没有指定sheet,尝试读取第一个sheet self.df = pd.read_excel( file_path, header=header_row ) # 尝试获取第一个sheet的名称 xl = pd.ExcelFile(file_path) if xl.sheet_names: self.current_sheet = xl.sheet_names[0] self.sheet_var.set(self.current_sheet) self.excel_path = file_path # 检查列是否存在 missing_cols = [] if id_col not in self.df.columns: missing_cols.append(f"ID列 '{id_col}'") if desc_col not in self.df.columns: missing_cols.append(f"功能点列 '{desc_col}'") if missing_cols: # 提供更详细的错误信息 available_cols = "\n".join(self.df.columns) error_msg = ( f"以下列不存在: {', '.join(missing_cols)}\n\n" f"可用列名:\n{available_cols}\n\n" "请检查表头设置是否正确(默认为第9)" ) raise ValueError(error_msg) # 如果状态列不存在,则创建 if status_col not in self.df.columns: self.df[status_col] = "否" # 默认未确认 # 更新列选择器 self.update_col_selector() # 更新Treeview列 self.update_treeview_columns() # 修复ID列显示问题 - 更健壮的处理 if id_col in self.df.columns: # 处理所有可能的缺失值 self.df[id_col] = self.df[id_col].fillna("") # 处理浮点数和整数类型 if self.df[id_col].dtype == float: # 将浮点数转换为整数再转字符串 self.df[id_col] = self.df[id_col].astype(int).astype(str) elif self.df[id_col].dtype == int: self.df[id_col] = self.df[id_col].astype(str) else: # 确保是字符串类型 self.df[id_col] = self.df[id_col].astype(str) # 替换可能的NaN字符串和空字符串 self.df[id_col] = self.df[id_col].replace(['nan', 'NaN', 'NaT', ''], '缺失ID') # 处理描述列显示问题 if desc_col in self.df.columns: # 填充缺失值 self.df[desc_col] = self.df[desc_col].fillna("无描述") # 确保是字符串类型 self.df[desc_col] = self.df[desc_col].astype(str) # 添加换符使长文本自动换 max_length = 100 # 每100个字符换 self.df[desc_col] = self.df[desc_col].apply( lambda x: '\n'.join([x[i:i+max_length] for i in range(0, len(x), max_length)])) # 添加数据到Treeview for i, row in self.df.iterrows(): status_value = row.get(status_col, "否") # 使用图标表示状态 status_icon = "✓" if status_value in ["是", "Y", "y", "Yes", "yes", "OK", "确认"] else "✗" status_text = f"{status_icon} {status_value}" # 获取ID值 id_value = str(row[id_col]) if id_col in row else "缺失ID" # 获取完整描述 full_desc = row[desc_col] if desc_col in row else "无描述" # 创建显示描述(截断过长的描述) display_desc = full_desc if len(full_desc) > max_desc_length: display_desc = full_desc[:max_desc_length] + "..." # 添加省略号 # 构建数据字典 row_data = { "selection": "☐", "id": id_value, "description": display_desc, "status": status_text } # 添加多列确认数据 for col in self.confirmation_columns: row_data[col] = row[col] if col in row else "" # 按列顺序构建值列表 ordered_values = [row_data.get(col, "") for col in self.column_order] # 确定样式标签 - 交替背景色 row_tag = "evenrow" if i % 2 == 0 else "oddrow" status_tag = "confirmed" if status_icon == "✓" else "pending" tags = (row_tag, status_tag) # 插入 item_id = self.tree.insert("", "end", values=ordered_values, tags=tags) # 存储完整描述 self.full_descriptions[item_id] = full_desc # 存储复选框状态 self.check_states[item_id] = (status_icon == "✓") # 每100更新一次界面 if i % 100 == 0: self.tree.update() self.master.update_idletasks() # 更新状态 self.status_var.set(f"成功加载: {len(self.df)} 条记录") # 调整列宽并刷新界面 self.adjust_columns() except Exception as e: # 显示详细的错误信息 error_msg = f"读取Excel失败: {str(e)}" self.status_var.set("加载失败") messagebox.showerror("加载错误", error_msg) logger.error(f"加载数据失败: {error_msg}\n{traceback.format_exc()}") def confirm_selected(self): """确认选中的功能点""" selected_items = [] for item_id in self.tree.get_children(): if self.check_states.get(item_id, False): selected_items.append(item_id) if not selected_items: messagebox.showinfo("提示", "请先选择功能点") return custom_status = self.custom_status_var.get().strip() or "OK" for item_id in selected_items: values = list(self.tree.item(item_id, "values")) # 找到状态列的位置 status_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "状态": status_col_idx = idx break # 更新状态列 if status_col_idx is not None and status_col_idx < len(values): values[status_col_idx] = f"✓ {custom_status}" # 更新多列确认 if self.confirmation_columns and self.df is not None: row_idx = self.tree.index(item_id) for col in self.confirmation_columns: if col in self.df.columns: # 找到列在Treeview中的位置 col_idx = self.column_order.index(col) if col_idx < len(values): values[col_idx] = custom_status self.df.at[row_idx, col] = custom_status # 更新Treeview self.tree.item(item_id, values=values) self.status_var.set(f"已确认 {len(selected_items)} 个功能点") # 自动保存 self.auto_save() def select_all(self): """全选功能点""" for item_id in self.tree.get_children(): values = list(self.tree.item(item_id, "values")) # 找到选择列的位置 select_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "选择": select_col_idx = idx break # 更新选择列 if select_col_idx is not None and select_col_idx < len(values): values[select_col_idx] = "☑" self.tree.item(item_id, values=values) self.check_states[item_id] = True self.update_row_status(item_id) self.status_var.set("已全选所有功能点") def deselect_all(self): """取消全选功能点""" for item_id in self.tree.get_children(): values = list(self.tree.item(item_id, "values")) # 找到选择列的位置 select_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "选择": select_col_idx = idx break # 更新选择列 if select_col_idx is not None and select_col_idx < len(values): values[select_col_idx] = "☐" self.tree.item(item_id, values=values) self.check_states[item_id] = False self.update_row_status(item_id) self.status_var.set("已取消全选所有功能点") def save_current_config(self): """保存当前配置""" self.config["id_col"] = self.id_col_var.get().strip() self.config["desc_col"] = self.desc_col_var.get().strip() self.config["status_col"] = self.status_col_var.get().strip() self.config["sheet_name"] = self.sheet_var.get() self.config["header_row"] = self.header_var.get() self.config["custom_status"] = self.custom_status_var.get().strip() self.config["confirmation_columns"] = self.confirmation_columns self.config["column_order"] = self.column_order self.config["max_desc_length"] = self.max_desc_var.get() # 保存最大描述长度 self.save_config() messagebox.showinfo("成功", "配置已保存") def auto_save(self): """自动保存功能""" if self.df is None or not self.excel_path: return try: # 获取当前配置 id_col = self.id_col_var.get().strip() desc_col = self.desc_col_var.get().strip() status_col = self.status_col_var.get().strip() # 更新DataFrame中的确认状态 for i, item_id in enumerate(self.tree.get_children()): # 获取Treeview中的状态值 values = self.tree.item(item_id, "values") # 找到状态列的位置 status_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "状态": status_col_idx = idx break if status_col_idx is not None and status_col_idx < len(values): status_value = values[status_col_idx] if status_value.startswith(("✓", "✗")): status_value = status_value[2:].strip() self.df.at[i, status_col] = status_value # 保存回Excel wb = openpyxl.load_workbook(self.excel_path) if self.current_sheet in wb.sheetnames: del wb[self.current_sheet] ws = wb.create_sheet(self.current_sheet) # 写入数据 for r, row in enumerate(dataframe_to_rows(self.df, index=False, header=True), 1): ws.append(row) wb.save(self.excel_path) self.status_var.set("数据已自动保存") except Exception as e: self.status_var.set("自动保存失败") logger.error(f"自动保存失败: {str(e)}") def save_to_excel(self): if self.df is None: messagebox.showerror("错误", "没有加载的数据") return try: # 执保存 self.auto_save() messagebox.showinfo("成功", f"数据已保存到:\n{self.excel_path}") self.status_var.set("数据保存成功") except Exception as e: messagebox.showerror("保存错误", f"写入Excel失败: {str(e)}\n请确保文件未被其他程序打开") logger.error(f"保存失败: {str(e)}") def main(): root = tk.Tk() # 设置应用图标 try: # 创建一个简单的蓝色方块作为图标 icon_data = """ R0lGODlhEAAQAIAAAP///wAAACH5BAEAAAAALAAAAAAQABAAAAIOhI+py+0Po5y02ouzPgUAOw== """ icon_img = tk.PhotoImage(data=icon_data) root.tk.call('wm', 'iconphoto', root._w, icon_img) except Exception as e: logger.warning(f"设置应用图标失败: {str(e)}") # 添加全局异常处理 def handle_exception(exc_type, exc_value, exc_traceback): error_msg = f"发生未捕获的异常:\n{exc_type.__name__}: {exc_value}" tb_str = "".join(traceback.format_tb(exc_traceback)) logger.error(f"未捕获的异常: {error_msg}\n{tb_str}") messagebox.showerror("严重错误", f"{error_msg}\n\n请查看日志获取详细信息") root.destroy() sys.excepthook = handle_exception app = ExcelControlPanel(root) root.mainloop() if __name__ == "__main__": # 强制使用UTF-8编码 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') main() 1、还是显示缺失ID,但实际上有ID,并且是整数 2、当鼠标点击到某一时,虽然程序没有崩溃,但是会报以下警告Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 540, in on_tree_leave self.tooltip.hide_tip() ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'hide_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 540, in on_tree_leave self.tooltip.hide_tip() ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'hide_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 540, in on_tree_leave self.tooltip.hide_tip() ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'hide_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "e:\system\Desktop\项目所需文件\工具\功能点确认工具\Excel_Function_Confirmation_System.py", line 536, in on_tree_motion self.tooltip.show_tip(full_desc, event.x, event.y) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'show_tip' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\cheny9210\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1968, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^
最新发布
08-20
import pandas as pd import tkinter as tk from tkinter import ttk, messagebox, filedialog import os import json import openpyxl from openpyxl.utils.dataframe import dataframe_to_rows from tkinter.font import Font import traceback import logging import sys import io # 配置日志系统 logging.basicConfig( filename='app.log', level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s' ) logger = logging.getLogger() class ScrollableFrame(ttk.Frame): """自定义可滚动框架实现""" def __init__(self, parent, *args, **kwargs): super().__init__(parent, *args, **kwargs) # 创建Canvas和滚动条 self.canvas = tk.Canvas(self, highlightthickness=0) self.scrollbar = ttk.Scrollbar(self, orient="vertical", command=self.canvas.yview) self.scrollable_frame = ttk.Frame(self.canvas) # 配置Canvas self.canvas.configure(yscrollcommand=self.scrollbar.set) self.canvas_frame = self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw") # 布局 self.canvas.pack(side="left", fill="both", expand=True, padx=0, pady=0) self.scrollbar.pack(side="right", fill="y") # 绑定事件 self.scrollable_frame.bind("<Configure>", self.on_frame_configure) self.canvas.bind("<Configure>", self.on_canvas_configure) self.canvas.bind_all("<MouseWheel>", self.on_mousewheel) def on_frame_configure(self, event): """当内部框架大小改变时更新滚动区域""" self.canvas.configure(scrollregion=self.canvas.bbox("all")) def on_canvas_configure(self, event): """当Canvas大小改变时调整内部框架宽度""" self.canvas.itemconfig(self.canvas_frame, width=event.width) def on_mousewheel(self, event): """鼠标滚轮滚动支持""" self.canvas.yview_scroll(int(-1*(event.delta/120)), "units") class ExcelControlPanel: def __init__(self, master): self.master = master master.title("功能点确认系统") master.geometry("1280x800") master.configure(bg="#f0f2f5") # 设置全局样式 self.set_styles() # 加载配置 self.config = self.load_config() # 初始化多列确认配置 self.confirmation_columns = self.config.get("confirmation_columns", []) self.column_order = self.config.get("column_order", []) # 创建界面元素 self.create_widgets() # 初始化数据 self.excel_path = "" self.df = None self.check_states = {} # 存储每个功能点的复选框状态 self.current_sheet = "" self.header_row = 0 # 更新列选择器 self.update_col_selector() def set_styles(self): """设置全局样式和字体""" style = ttk.Style() style.theme_use('clam') # 自定义字体 self.title_font = Font(family="Microsoft YaHei", size=16, weight="bold") self.subtitle_font = Font(family="Microsoft YaHei", size=10) self.normal_font = Font(family="Microsoft YaHei", size=9) # 配置样式 style.configure("TFrame", background="#f0f2f5") style.configure("TLabel", font=self.normal_font, background="#f0f2f5", foreground="#333") style.configure("TButton", font=self.normal_font, padding=8) # Treeview样式 style.configure("Treeview.Heading", font=self.subtitle_font, background="#4a76b5", foreground="white") style.configure("Treeview", font=self.normal_font, rowheight=40, background="white", fieldbackground="white") # 状态栏样式 style.configure("Status.TFrame", background="#e0e0e0") style.configure("Status.TLabel", font=self.normal_font, background="#4a76b5", foreground="#ffffff", padding=5) # 卡片样式 style.configure("Card.TFrame", background="white", borderwidth=0, relief="solid", padding=10, bordercolor="#e1e4e8") style.configure("Card.TLabelframe", background="white", borderwidth=1, relief="solid", padding=10, bordercolor="#e1e4e8") style.configure("Card.TLabelframe.Label", font=self.subtitle_font, foreground="#2c3e50", background="white") # 按钮样式 style.map("Primary.TButton", background=[("active", "#3a66a5"), ("pressed", "#2a5685")], foreground=[("active", "white"), ("pressed", "white")]) style.configure("Primary.TButton", background="#4a76b5", foreground="white", font=self.subtitle_font, borderwidth=0) style.map("Success.TButton", background=[("active", "#28a745"), ("pressed", "#218838")], foreground=[("active", "white"), ("pressed", "white")]) style.configure("Success.TButton", background="#28a745", foreground="white", font=self.subtitle_font, borderwidth=0) style.map("Danger.TButton", background=[("active", "#dc3545"), ("pressed", "#c82333")], foreground=[("active", "white"), ("pressed", "white")]) style.configure("Danger.TButton", background="#dc3545", foreground="white", font=self.subtitle_font, borderwidth=0) # 输入框样式 style.configure("Custom.TEntry", fieldbackground="#f8f9fa", bordercolor="#ced4da") def load_config(self): """加载配置文件""" config_path = "excel_config.json" default_config = { "id_col": "No.", "desc_col": "レビュー観点(CHN)", "status_col": "レビュー結果", "sheet_name": "", "header_row": 9, "last_dir": os.getcwd(), "custom_status": "OK", "confirmation_columns": [], "column_order": ["selection", "id", "description", "status"] } if os.path.exists(config_path): try: with open(config_path, 'r', encoding='utf-8') as f: config = json.load(f) # 确保所有键都存在 for key in default_config: if key not in config: config[key] = default_config[key] return config except Exception as e: logger.error(f"加载配置文件失败: {str(e)}") return default_config return default_config def save_config(self): """保存配置文件""" config_path = "excel_config.json" try: with open(config_path, 'w', encoding='utf-8') as f: json.dump(self.config, f, ensure_ascii=False, indent=2) except Exception as e: logger.error(f"保存配置文件失败: {str(e)}") def create_widgets(self): """创建现代化界面元素""" # 主容器 main_container = ttk.Frame(self.master, style="Card.TFrame") main_container.pack(fill="both", expand=True, padx=20, pady=20) # 标题栏 title_frame = ttk.Frame(main_container, style="Title.TFrame") title_frame.pack(fill="x", pady=(0, 20)) ttk.Label(title_frame, text="功能点确认系统", font=self.title_font, background="#4a76b5", foreground="white", padding=10).pack(side="left", fill="x", expand=True) # 主内容区域 content_frame = ttk.Frame(main_container) content_frame.pack(fill="both", expand=True) # 左侧控制面板 - 可滚动 control_container = ttk.Frame(content_frame, width=350) control_container.pack(side="left", fill="y", padx=(0, 20)) # 创建自定义可滚动框架 scrollable_frame = ScrollableFrame(control_container, width=350) scrollable_frame.pack(fill="both", expand=True) # 获取内部框架 inner_frame = scrollable_frame.scrollable_frame # 美化控制面板 control_card = ttk.LabelFrame(inner_frame, text="控制面板", style="Card.TLabelframe") control_card.pack(fill="both", expand=True, padx=10, pady=10) # 文件选择区域 file_frame = ttk.LabelFrame(control_card, text="Excel文件设置") file_frame.pack(fill="x", padx=10, pady=(0, 15)) ttk.Label(file_frame, text="Excel文件路径:").pack(anchor="w", pady=(0, 5), padx=10) path_frame = ttk.Frame(file_frame) path_frame.pack(fill="x", pady=5, padx=10) self.path_entry = ttk.Entry(path_frame, width=30, style="Custom.TEntry") self.path_entry.pack(side="left", fill="x", expand=True, padx=(0, 5)) ttk.Button(path_frame, text="浏览", command=self.browse_file, width=8).pack(side="left") ttk.Button(file_frame, text="加载数据", command=self.load_data, style="Primary.TButton").pack(fill="x", pady=10, padx=10) # Sheet选择区域 sheet_frame = ttk.LabelFrame(control_card, text="工作表设置") sheet_frame.pack(fill="x", padx=10, pady=(0, 15)) ttk.Label(sheet_frame, text="当前Sheet:").pack(anchor="w", padx=10, pady=(10, 0)) self.sheet_var = tk.StringVar(value=self.config.get("sheet_name", "未选择")) sheet_display = ttk.Label(sheet_frame, textvariable=self.sheet_var, font=self.subtitle_font) sheet_display.pack(anchor="w", pady=(0, 10), padx=10) ttk.Button(sheet_frame, text="选择工作表", command=self.select_sheet, style="Primary.TButton").pack(fill="x", padx=10, pady=(0, 10)) # 表头设置 header_frame = ttk.LabelFrame(control_card, text="表头设置") header_frame.pack(fill="x", padx=10, pady=(0, 15)) ttk.Label(header_frame, text="表头所在:").pack(anchor="w", padx=10, pady=(10, 0)) self.header_var = tk.IntVar(value=self.config.get("header_row", 9)) ttk.Entry(header_frame, textvariable=self.header_var, width=10, style="Custom.TEntry").pack(anchor="w", pady=5, padx=10) # 列名配置区域 col_frame = ttk.LabelFrame(control_card, text="列名配置") col_frame.pack(fill="x", padx=10, pady=(0, 15)) # ID列 ttk.Label(col_frame, text="ID列名:").pack(anchor="w", padx=10, pady=(10, 0)) self.id_col_var = tk.StringVar(value=self.config["id_col"]) ttk.Entry(col_frame, textvariable=self.id_col_var, style="Custom.TEntry").pack(fill="x", pady=(0, 10), padx=10) # 功能点列 ttk.Label(col_frame, text="功能点列名:").pack(anchor="w", padx=10, pady=(0, 0)) self.desc_col_var = tk.StringVar(value=self.config["desc_col"]) ttk.Entry(col_frame, textvariable=self.desc_col_var, style="Custom.TEntry").pack(fill="x", pady=(0, 10), padx=10) # 状态列 ttk.Label(col_frame, text="状态列名:").pack(anchor="w", padx=10, pady=(0, 0)) self.status_col_var = tk.StringVar(value=self.config["status_col"]) ttk.Entry(col_frame, textvariable=self.status_col_var, style="Custom.TEntry").pack(fill="x", pady=(0, 10), padx=10) # 自定义状态 ttk.Label(col_frame, text="自定义确认状态:").pack(anchor="w", padx=10, pady=(0, 0)) self.custom_status_var = tk.StringVar(value=self.config.get("custom_status", "OK")) ttk.Entry(col_frame, textvariable=self.custom_status_var, style="Custom.TEntry").pack(fill="x", pady=(0, 10), padx=10) # 多列确认设置 multi_col_frame = ttk.LabelFrame(control_card, text="多列确认设置") multi_col_frame.pack(fill="x", padx=10, pady=(0, 15)) # 列选择器 ttk.Label(multi_col_frame, text="选择要确认的列:").pack(anchor="w", padx=10, pady=(10, 0)) col_selector_frame = ttk.Frame(multi_col_frame) col_selector_frame.pack(fill="x", pady=5, padx=10) self.col_selector = ttk.Combobox(col_selector_frame, state="readonly", width=15) self.col_selector.pack(side="left", fill="x", expand=True, padx=(0, 5)) # 添加/移除按钮 ttk.Button(col_selector_frame, text="添加", command=self.add_confirmation_column, width=8).pack(side="left") # 已选列列表 ttk.Label(multi_col_frame, text="已选确认列:").pack(anchor="w", padx=10, pady=(10, 0)) self.selected_cols_listbox = tk.Listbox(multi_col_frame, height=3, font=self.normal_font, bg="#f8f9fa", highlightthickness=0) self.selected_cols_listbox.pack(fill="x", pady=5, padx=10) # 加载已配置的确认列 for col in self.confirmation_columns: self.selected_cols_listbox.insert(tk.END, col) # 移除按钮 remove_btn = ttk.Button(multi_col_frame, text="移除选中列", command=self.remove_confirmation_column) remove_btn.pack(fill="x", pady=(0, 10), padx=10) # 操作按钮区域 btn_frame = ttk.Frame(control_card) btn_frame.pack(fill="x", padx=5, pady=10) ttk.Button(btn_frame, text="保存配置", command=self.save_current_config, style="Success.TButton").pack(fill="x", pady=5) ttk.Button(btn_frame, text="确认选中项", command=self.confirm_selected, style="Primary.TButton").pack(fill="x", pady=5) ttk.Button(btn_frame, text="全选", command=self.select_all, style="Primary.TButton").pack(side="left", fill="x", expand=True, pady=5) ttk.Button(btn_frame, text="取消全选", command=self.deselect_all, style="Danger.TButton").pack(side="left", fill="x", expand=True, pady=5) ttk.Button(btn_frame, text="保存到Excel", command=self.save_to_excel, style="Success.TButton").pack(fill="x", pady=5) # 数据显示区域 data_card = ttk.LabelFrame(content_frame, text="功能点列表", style="Card.TLabelframe") data_card.pack(side="right", fill="both", expand=True) # Treeview容器 tree_frame = ttk.Frame(data_card) tree_frame.pack(fill="both", expand=True, padx=5, pady=5) # 创建Treeview self.tree = ttk.Treeview(tree_frame, columns=[], show="headings", height=20) # 滚动条设置 vsb = ttk.Scrollbar(tree_frame, orient="vertical", command=self.tree.yview) hsb = ttk.Scrollbar(tree_frame, orient="horizontal", command=self.tree.xview) self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set) # 使用grid布局管理器 self.tree.grid(row=0, column=0, sticky="nsew") vsb.grid(row=0, column=1, sticky="ns") hsb.grid(row=1, column=0, sticky="ew") # 配置网格权重 tree_frame.columnconfigure(0, weight=1) tree_frame.rowconfigure(0, weight=1) # 添加标签样式 self.tree.tag_configure("confirmed", background="#d4edda") self.tree.tag_configure("pending", background="#f8d7da") # 绑定列调整事件 self.tree.bind("<Configure>", self.on_tree_configure) # 绑定点击事件处理复选框 self.tree.bind("<Button-1>", self.on_tree_click) # 详情区域 detail_frame = ttk.LabelFrame(data_card, text="功能点详情", style="Card.TLabelframe") detail_frame.pack(fill="x", padx=5, pady=(0, 5)) self.detail_text = tk.Text(detail_frame, wrap="word", font=self.normal_font, height=3, bg="#f8f9fa", relief="solid", borderwidth=1) scroll_detail = ttk.Scrollbar(detail_frame, command=self.detail_text.yview) self.detail_text.config(yscrollcommand=scroll_detail.set) # 使用grid布局文本框和滚动条 self.detail_text.grid(row=0, column=0, sticky="nsew", padx=5, pady=5) scroll_detail.grid(row=0, column=1, sticky="ns", pady=5) # 配置网格权重 detail_frame.columnconfigure(0, weight=1) detail_frame.rowconfigure(0, weight=1) self.detail_text.config(state="disabled") # 绑定Treeview选择事件 self.tree.bind("<<TreeviewSelect>>", self.on_tree_select) # 状态栏 self.status_var = tk.StringVar() self.status_var.set("就绪 - 请选择Excel文件开始") # 创建状态栏容器框架 status_container = ttk.Frame(self.master, style="Status.TFrame") status_container.pack(side="bottom", fill="x", padx=0, pady=0) # 状态标签 status_bar = ttk.Label( status_container, textvariable=self.status_var, style="Status.TLabel", anchor="w", padding=(10, 5, 10, 5) ) status_bar.pack(side="bottom", fill="x", expand=False) # 初始化Treeview列 self.update_treeview_columns() def update_treeview_columns(self): """更新Treeview列""" # 基础列 base_columns = ["selection", "id", "description", "status"] # 所有列 = 基础列 + 确认列 all_columns = base_columns + self.confirmation_columns # 如果列顺序未设置或长度不匹配,则重置 if not self.column_order or len(self.column_order) != len(all_columns): self.column_order = all_columns # 重新配置Treeview self.tree.configure(columns=self.column_order) # 设置列标题 column_titles = { "selection": "选择", "id": "ID", "description": "功能点", "status": "状态" } for col in self.column_order: if col in column_titles: self.tree.heading(col, text=column_titles[col]) else: self.tree.heading(col, text=col) # 设置列属性 if col == "selection": self.tree.column(col, width=50, anchor="center") elif col == "id": self.tree.column(col, width=100, anchor="center") elif col == "description": self.tree.column(col, width=400, anchor="w", stretch=True) elif col == "status": self.tree.column(col, width=100, anchor="center") else: # 确认列 self.tree.column(col, width=100, anchor="center") # 调整列宽 self.adjust_columns() def on_tree_configure(self, event): """Treeview大小改变时调整列宽""" self.adjust_columns() def on_tree_select(self, event): """当Treeview中选中时,显示功能点详情""" selected_items = self.tree.selection() if not selected_items: return item = selected_items[0] # 获取功能点描述 values = self.tree.item(item, "values") # 找到描述列的索引 desc_idx = None for idx, col in enumerate(self.tree["columns"]): if self.tree.heading(col, "text") == "功能点": desc_idx = idx break if desc_idx is not None and desc_idx < len(values): desc = values[desc_idx] self.detail_text.config(state="normal") self.detail_text.delete(1.0, tk.END) self.detail_text.insert(tk.END, desc) self.detail_text.config(state="disabled") def on_tree_click(self, event): """处理Treeview点击事件,切换复选框状态""" region = self.tree.identify("region", event.x, event.y) if region == "cell": column = self.tree.identify_column(event.x) item = self.tree.identify_row(event.y) if not item: return # 获取列索引 col_idx = int(column[1:]) - 1 columns = self.tree["columns"] # 确保索引有效 if col_idx >= len(columns): return # 获取列标识符 col_id = columns[col_idx] # 只处理"选择"列 if self.tree.heading(col_id, "text") == "选择": # 获取当前状态 values = list(self.tree.item(item, "values")) current_state = values[col_idx] # 切换状态 if current_state == "☐": new_state = "☑" else: new_state = "☐" # 更新Treeview values[col_idx] = new_state self.tree.item(item, values=values) # 更新状态存储 self.check_states[item] = (new_state == "☑") # 更新的整体状态显示 self.update_row_status(item) def update_row_status(self, item_id): """根据复选框状态更新状态显示""" values = list(self.tree.item(item_id, "values")) is_checked = self.check_states.get(item_id, False) # 找到状态列的位置 status_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "状态": status_col_idx = idx break if status_col_idx is not None and status_col_idx < len(values): if is_checked: values[status_col_idx] = "✓ 待确认" self.tree.item(item_id, tags=("confirmed",)) else: values[status_col_idx] = "✗ 未确认" self.tree.item(item_id, tags=("pending",)) self.tree.item(item_id, values=values) def adjust_columns(self, event=None): """根据窗口大小自动调整列宽""" if not self.tree.winfo_exists(): return width = self.tree.winfo_width() if width < 100: # 防止宽度过小 return # 计算可用宽度 available_width = width - 20 # 减去滚动条宽度 # 设置列宽比例 column_weights = { "selection": 0.05, "id": 0.15, "description": 0.55, # 增加功能点列的比例 "status": 0.1 } # 设置基础列宽 for col, weight in column_weights.items(): if col in self.tree["columns"]: col_width = int(available_width * weight) self.tree.column(col, width=col_width) # 设置确认列宽 if self.confirmation_columns: confirm_col_width = int(available_width * 0.08) # 减小确认列宽度 for col in self.confirmation_columns: if col in self.tree["columns"]: self.tree.column(col, width=confirm_col_width) def update_col_selector(self): """更新列选择器""" if self.df is not None: self.col_selector["values"] = list(self.df.columns) def add_confirmation_column(self): """添加确认列""" col = self.col_selector.get() if col and col not in self.confirmation_columns: self.confirmation_columns.append(col) self.selected_cols_listbox.insert(tk.END, col) self.update_treeview_columns() def remove_confirmation_column(self): """移除确认列""" selection = self.selected_cols_listbox.curselection() if selection: index = selection[0] self.confirmation_columns.pop(index) self.selected_cols_listbox.delete(index) self.update_treeview_columns() def browse_file(self): initial_dir = self.config.get("last_dir", os.getcwd()) file_path = filedialog.askopenfilename( initialdir=initial_dir, filetypes=[("Excel文件", "*.xlsx;*.xls")] ) if file_path: self.path_entry.delete(0, tk.END) self.path_entry.insert(0, file_path) # 更新最后访问目录 self.config["last_dir"] = os.path.dirname(file_path) self.save_config() def select_sheet(self): """选择工作表""" file_path = self.path_entry.get() if not file_path or not os.path.exists(file_path): messagebox.showerror("错误", "请先选择有效的Excel文件") return try: # 获取所有sheet名称 xl = pd.ExcelFile(file_path) sheet_names = xl.sheet_names # 创建选择对话框 sheet_dialog = tk.Toplevel(self.master) sheet_dialog.title("选择工作表") sheet_dialog.geometry("400x300") sheet_dialog.transient(self.master) sheet_dialog.grab_set() sheet_dialog.configure(bg="#f5f7fa") ttk.Label(sheet_dialog, text="请选择工作表:", font=self.subtitle_font, background="#f5f7fa").pack(pady=10) # 使用Treeview显示工作表 sheet_tree = ttk.Treeview(sheet_dialog, columns=("名称",), show="headings", height=8) sheet_tree.heading("名称", text="工作表名称") sheet_tree.column("名称", width=350) sheet_tree.pack(fill="both", expand=True, padx=20, pady=5) for name in sheet_names: sheet_tree.insert("", "end", values=(name,)) # 按钮框架 btn_frame = ttk.Frame(sheet_dialog) btn_frame.pack(fill="x", padx=20, pady=10) def on_select(): selected = sheet_tree.selection() if selected: self.current_sheet = sheet_tree.item(selected[0], "values")[0] self.sheet_var.set(self.current_sheet) # 保存工作表名称到配置 self.config["sheet_name"] = self.current_sheet self.save_config() sheet_dialog.destroy() ttk.Button(btn_frame, text="取消", command=sheet_dialog.destroy).pack(side="right", padx=5) ttk.Button(btn_frame, text="确定", command=on_select, style="Primary.TButton").pack(side="right") except Exception as e: messagebox.showerror("错误", f"读取Excel失败: {str(e)}") logger.error(f"选择工作表失败: {str(e)}") def load_data(self): file_path = self.path_entry.get() if not file_path or not os.path.exists(file_path): messagebox.showerror("错误", "无效的文件路径") return # 在状态栏显示加载中 self.status_var.set("正在加载数据...") self.master.update() # 强制更新界面 # 清空Treeview for item in self.tree.get_children(): self.tree.delete(item) self.check_states = {} # 获取当前配置 id_col = self.id_col_var.get().strip() desc_col = self.desc_col_var.get().strip() status_col = self.status_col_var.get().strip() sheet_name = self.sheet_var.get() or None header_row = self.header_var.get() - 1 # pandas header是0-based索引 # 直接调用加载任务 self.load_task(file_path, id_col, desc_col, status_col, sheet_name, header_row) def load_task(self, file_path, id_col, desc_col, status_col, sheet_name, header_row): try: # 确保使用正确的sheet_name if not sheet_name and self.config.get("sheet_name"): sheet_name = self.config["sheet_name"] # 从配置获取工作表名 # 读取Excel文件 if sheet_name: self.df = pd.read_excel( file_path, sheet_name=sheet_name, header=header_row ) else: # 如果没有指定sheet,尝试读取第一个sheet self.df = pd.read_excel( file_path, header=header_row ) # 尝试获取第一个sheet的名称 xl = pd.ExcelFile(file_path) if xl.sheet_names: self.current_sheet = xl.sheet_names[0] self.sheet_var.set(self.current_sheet) self.excel_path = file_path # 检查列是否存在 missing_cols = [] if id_col not in self.df.columns: missing_cols.append(f"ID列 '{id_col}'") if desc_col not in self.df.columns: missing_cols.append(f"功能点列 '{desc_col}'") if missing_cols: # 提供更详细的错误信息 available_cols = "\n".join(self.df.columns) error_msg = ( f"以下列不存在: {', '.join(missing_cols)}\n\n" f"可用列名:\n{available_cols}\n\n" "请检查表头设置是否正确(默认为第9)" ) raise ValueError(error_msg) # 如果状态列不存在,则创建 if status_col not in self.df.columns: self.df[status_col] = "否" # 默认未确认 # 更新列选择器 self.update_col_selector() # 更新Treeview列 self.update_treeview_columns() # 处理ID列NaN值 if id_col in self.df.columns: if self.df[id_col].dtype == float: # 处理浮点型NaN self.df[id_col] = self.df[id_col].fillna(-1).astype(int).astype(str) self.df[id_col] = self.df[id_col].replace("-1", "缺失ID") else: # 处理其他类型的NaN self.df[id_col] = self.df[id_col].fillna("缺失ID").astype(str) # 处理描述列NaN值 if desc_col in self.df.columns: self.df[desc_col] = self.df[desc_col].fillna("无描述").astype(str) # 添加数据到Treeview for i, row in self.df.iterrows(): status_value = row.get(status_col, "否") # 使用图标表示状态 status_icon = "✓" if status_value in ["是", "Y", "y", "Yes", "yes", "OK", "确认"] else "✗" status_text = f"{status_icon} {status_value}" tag = "confirmed" if status_icon == "✓" else "pending" # 构建数据字典 row_data = { "selection": "☐", "id": str(row[id_col]) if id_col in row else "缺失ID", "description": row[desc_col] if desc_col in row else "无描述", "status": status_text } # 添加多列确认数据 for col in self.confirmation_columns: row_data[col] = row[col] if col in row else "" # 按列顺序构建值列表 ordered_values = [row_data.get(col, "") for col in self.column_order] # 插入 item_id = self.tree.insert("", "end", values=ordered_values, tags=(tag,)) # 存储复选框状态 self.check_states[item_id] = (status_icon == "✓") # 每100更新一次界面 if i % 100 == 0: self.tree.update() self.master.update_idletasks() # 更新状态 self.status_var.set(f"成功加载: {len(self.df)} 条记录") # 调整列宽并刷新界面 self.adjust_columns() except Exception as e: # 显示详细的错误信息 error_msg = f"读取Excel失败: {str(e)}" self.status_var.set("加载失败") messagebox.showerror("加载错误", error_msg) logger.error(f"加载数据失败: {error_msg}\n{traceback.format_exc()}") def confirm_selected(self): """确认选中的功能点""" selected_items = [] for item_id in self.tree.get_children(): if self.check_states.get(item_id, False): selected_items.append(item_id) if not selected_items: messagebox.showinfo("提示", "请先选择功能点") return custom_status = self.custom_status_var.get().strip() or "OK" for item_id in selected_items: values = list(self.tree.item(item_id, "values")) # 找到状态列的位置 status_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "状态": status_col_idx = idx break # 更新状态列 if status_col_idx is not None and status_col_idx < len(values): values[status_col_idx] = f"✓ {custom_status}" # 更新多列确认 if self.confirmation_columns and self.df is not None: row_idx = self.tree.index(item_id) for col in self.confirmation_columns: if col in self.df.columns: # 找到列在Treeview中的位置 col_idx = self.column_order.index(col) if col_idx < len(values): values[col_idx] = custom_status self.df.at[row_idx, col] = custom_status # 更新Treeview self.tree.item(item_id, values=values, tags=("confirmed",)) self.status_var.set(f"已确认 {len(selected_items)} 个功能点") # 自动保存 self.auto_save() def select_all(self): """全选功能点""" for item_id in self.tree.get_children(): values = list(self.tree.item(item_id, "values")) # 找到选择列的位置 select_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "选择": select_col_idx = idx break # 更新选择列 if select_col_idx is not None and select_col_idx < len(values): values[select_col_idx] = "☑" self.tree.item(item_id, values=values) self.check_states[item_id] = True self.update_row_status(item_id) self.status_var.set("已全选所有功能点") def deselect_all(self): """取消全选功能点""" for item_id in self.tree.get_children(): values = list(self.tree.item(item_id, "values")) # 找到选择列的位置 select_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "选择": select_col_idx = idx break # 更新选择列 if select_col_idx is not None and select_col_idx < len(values): values[select_col_idx] = "☐" self.tree.item(item_id, values=values) self.check_states[item_id] = False self.update_row_status(item_id) self.status_var.set("已取消全选所有功能点") def save_current_config(self): """保存当前配置""" self.config["id_col"] = self.id_col_var.get().strip() self.config["desc_col"] = self.desc_col_var.get().strip() self.config["status_col"] = self.status_col_var.get().strip() self.config["sheet_name"] = self.sheet_var.get() self.config["header_row"] = self.header_var.get() self.config["custom_status"] = self.custom_status_var.get().strip() self.config["confirmation_columns"] = self.confirmation_columns self.config["column_order"] = self.column_order self.save_config() messagebox.showinfo("成功", "配置已保存") def auto_save(self): """自动保存功能""" if self.df is None or not self.excel_path: return try: # 获取当前配置 id_col = self.id_col_var.get().strip() desc_col = self.desc_col_var.get().strip() status_col = self.status_col_var.get().strip() # 更新DataFrame中的确认状态 for i, item_id in enumerate(self.tree.get_children()): # 获取Treeview中的状态值 values = self.tree.item(item_id, "values") # 找到状态列的位置 status_col_idx = None for idx, col_id in enumerate(self.tree["columns"]): if self.tree.heading(col_id, "text") == "状态": status_col_idx = idx break if status_col_idx is not None and status_col_idx < len(values): status_value = values[status_col_idx] if status_value.startswith(("✓", "✗")): status_value = status_value[2:].strip() self.df.at[i, status_col] = status_value # 保存回Excel wb = openpyxl.load_workbook(self.excel_path) if self.current_sheet in wb.sheetnames: del wb[self.current_sheet] ws = wb.create_sheet(self.current_sheet) # 写入数据 for r, row in enumerate(dataframe_to_rows(self.df, index=False, header=True), 1): ws.append(row) wb.save(self.excel_path) self.status_var.set("数据已自动保存") except Exception as e: self.status_var.set("自动保存失败") logger.error(f"自动保存失败: {str(e)}") def save_to_excel(self): if self.df is None: messagebox.showerror("错误", "没有加载的数据") return try: # 执保存 self.auto_save() messagebox.showinfo("成功", f"数据已保存到:\n{self.excel_path}") self.status_var.set("数据保存成功") except Exception as e: messagebox.showerror("保存错误", f"写入Excel失败: {str(e)}\n请确保文件未被其他程序打开") logger.error(f"保存失败: {str(e)}") def main(): root = tk.Tk() # 设置应用图标 try: # 创建一个简单的蓝色方块作为图标 icon_data = """ R0lGODlhEAAQAIAAAP///wAAACH5BAEAAAAALAAAAAAQABAAAAIOhI+py+0Po5y02ouzPgUAOw== """ icon_img = tk.PhotoImage(data=icon_data) root.tk.call('wm', 'iconphoto', root._w, icon_img) except Exception as e: logger.warning(f"设置应用图标失败: {str(e)}") # 添加全局异常处理 def handle_exception(exc_type, exc_value, exc_traceback): error_msg = f"发生未捕获的异常:\n{exc_type.__name__}: {exc_value}" tb_str = "".join(traceback.format_tb(exc_traceback)) logger.error(f"未捕获的异常: {error_msg}\n{tb_str}") messagebox.showerror("严重错误", f"{error_msg}\n\n请查看日志获取详细信息") root.destroy() sys.excepthook = handle_exception app = ExcelControlPanel(root) root.mainloop() if __name__ == "__main__": # 强制使用UTF-8编码 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') main() 所有的都挺好的,就是功能点的那一列,字太多了没有办法全部显示出来,请根据字体多少,将所有的字全部显示出来
08-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值