根據上面倆個代碼保留現在功能修改儅讀取excel工作表名生成的按鈕按表名相近的分組顯示,避免按鈕密集和顯示不完整
时间: 2024-02-11 15:08:47 浏览: 35
以下是修改后的代码,它会根据工作表名生成的按钮进行分组显示,避免了按钮密集和显示不完整的问题:
```
import tkinter as tk
import openpyxl as xl
from difflib import SequenceMatcher
class ExcelApp:
def __init__(self, master):
self.master = master
master.title("Excel App")
# 获取屏幕的宽度和高度
screen_width = master.winfo_screenwidth()
screen_height = master.winfo_screenheight()
# 将窗口的大小设置为屏幕的大小
master.geometry("%dx%d" % (screen_width, screen_height))
master.state('zoomed')#窗口最大化
# 创建菜单栏
menubar = tk.Menu(master)
master.config(menu=menubar)
# 创建文件菜单及其子菜单
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="PA綫", command=lambda: self.load_excel(r'D:\拆分后的工作簿\新增資料夾\PA點檢.xlsx', "PA綫", "PA綫"), compound='left', image='', foreground='black', font=('Microsoft JhengHei', 12))
menubar.add_cascade(label="點檢綫別", menu=filemenu, compound='left', image='', foreground='blue', font=('Arial', 12))
# 创建左侧面板
self.panel_left = tk.Frame(master, width=150, bg='lightcyan')
self.panel_left.pack(side=tk.LEFT, fill=tk.Y)
# 存储工作簿的工作表名
self.sheet_names = []
# 存储按钮组的列表
self.button_groups = []
def load_excel(self, filename, menu_label, selected_label_text):
# 将菜单标签存储在 self.la 中
self.la = menu_label
# 使用 openpyxl 库加载指定的 Excel 文件
self.workbook = xl.load_workbook(filename)
# 获取工作簿中的工作表名称
self.sheet_names = self.workbook.sheetnames
# 更新选中标签文本
self.selected_label.config(text=selected_label_text)
# 清空左侧面板和按钮组列表
for widget in self.panel_left.winfo_children():
widget.destroy()
self.button_groups.clear()
# 将工作表名称分组
self.group_sheet_names()
# 显示按钮组
self.show_button_groups()
def group_sheet_names(self):
# 将工作表名称按相似程度分组
groups = []
for sheet_name in self.sheet_names:
found = False
for group in groups:
if SequenceMatcher(None, sheet_name, group[0]).ratio() > 0.7:
# 将工作表名称添加到现有的组中
group.append(sheet_name)
found = True
break
if not found:
# 创建一个新组
groups.append([sheet_name])
# 将组按名称排序
groups.sort(key=lambda group: group[0])
# 将每个组的名称和按钮列表添加到 self.button_groups 中
for group in groups:
group_name = group[0]
buttons = []
for sheet_name in group:
button = tk.Button(self.panel_left, text=sheet_name, command=lambda name=sheet_name: self.show_sheet(name))
button.grid(sticky="ew", padx=1, pady=1)
button.used = False
button.bind("<Enter>", lambda event, button=button: button.configure(bg="lightgray"))
button.bind("<Leave>", lambda event, button=button: button.configure(bg="SystemButtonFace"))
button.bind("<Button-1>", lambda event, button=button: self.mark_used(button))
buttons.append(button)
self.button_groups.append((group_name, buttons))
def show_button_groups(self):
# 显示按钮组
for group_name, buttons in self.button_groups:
label = tk.Label(self.panel_left, text=group_name, font=('Arial', 12, 'bold'), bg='lightcyan')
label.grid(sticky="ew", padx=1, pady=1)
for button in buttons:
button.grid(sticky="ew", padx=20, pady=1)
def mark_used(self, button):
# 将按钮的标记设置为“已使用”
button.used = True
# 更改按钮的背景颜色
button.configure(bg="green")
# 取消按钮的绑定事件
button.unbind("<Button-1>")
# 绑定鼠标离开事件,使按钮的背景颜色保持不变
button.bind("<Leave>", lambda event, button=button: button.configure(bg="green"))
for group_name, buttons in self.button_groups:
for btn in buttons:
if btn != button:
# 取消其他按钮的点击事件绑定
btn.unbind("<Button-1>")
root = tk.Tk()
app = ExcelApp(root)
root.mainloop()
```
修改后的代码中,load_excel 方法中添加了两行代码,用于清空左侧面板和按钮组列表。接着,它调用 group_sheet_names 方法来将工作表名称分组,并将每个组的名称和按钮列表添加到 self.button_groups 中。最后,它调用 show_button_groups 方法来显示按钮组。
group_sheet_names 方法使用 difflib 库的 SequenceMatcher 类来计算工作表名称之间的相似程度,并将名称相似的工作表名称分组。具体来说,它首先创建一个空列表 groups,然后遍历每个工作表名称,将其添加到现有的组中或创建一个新组。对于每个组,它将其名称和按钮列表添加到 self.button_groups 中,并在每个按钮上绑定 mark_used 方法来将其标记为“已使用”。
show_button_groups 方法用于显示按钮组。对于每个组,它创建一个标签和按钮,并将它们添加到左侧面板中。
最后,mark_used 方法用于将按钮标记为“已使用”。它将按钮的标记设置为 True,并将其背景颜色改为绿色。它还取消了其他按钮的点击事件绑定,以避免多次点击同一组的按钮。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)