编写程序实现中文级联菜单,建议可以使用pypinyin或其它扩展库。
import tkinter as tk
from tkinter import ttk
from pypinyin import pinyin, Style
# 示例数据
menu_data = {
"水果": \["苹果", "香蕉", "橙子"\],
"动物": \["狗", "猫", "兔子"\],
"城市": \["北京", "上海", "广州"\]
}
def create_menu(menu_data):
root = tk.Tk()
root.title("中文级联菜单")
main_menu = tk.Menu(root)
root.config(menu=main_menu)
for category, items in menu_data.items():
category_menu = tk.Menu(main_menu, tearoff=0)
main_menu.add_cascade(label=category, menu=category_menu)
for item in sorted(items, key=lambda x: ''.join(pinyin(x, style=Style.NORMAL))):
category_menu.add_command(label=item, command=lambda i=item: on_select(i))
root.mainloop()
def on_select(item):
print(f"你选择了: {item}")
if _name_ == "_main_":
create_menu(menu_data)
标签:category,级联,Python0036,menu,item,菜单,tk,main,root
From: https://www.cnblogs.com/yuanxinglan/p/18214340