5.9

中文级菜单(python)

编写程序实现中文级联菜单,建议可以使用pypinyin或其它扩展库。

复制代码
import tkinter as tk

from tkinter import ttk

from pypinyin import lazy_pinyin

# 省份、城市、地区数据

data = {

    "北京": {

        "北京市": ["东城区", "西城区", "朝阳区"],

    },

    "上海": {

        "上海市": ["黄浦区", "静安区", "徐汇区"],

    },

    "广东": {

        "广州市": ["越秀区", "荔湾区", "天河区"],

        "深圳市": ["福田区", "南山区", "罗湖区"],

        "珠海市": ["香洲区", "斗门区", "金湾区"],

    },

    "浙江": {

        "杭州市": ["西湖区", "拱墅区", "下城区"],

        "宁波市": ["海曙区", "江北区", "镇海区"],

        "温州市": ["鹿城区", "龙湾区", "瓯海区"],

    }

}

class ChineseCascadeMenu(tk.Tk):

    def __init__(self):

        super().__init__()

        self.title("中文级联菜单")

        

        self.province_var = tk.StringVar(self)

        self.city_var = tk.StringVar(self)

        self.district_var = tk.StringVar(self)

        

        self.create_widgets()

    def create_widgets(self):

        # 省份级联菜单

        self.province_label = ttk.Label(self, text="选择省份:")

        self.province_label.grid(row=0, column=0, padx=5, pady=5)

        

        self.province_combo = ttk.Combobox(self, textvariable=self.province_var, state="readonly")

        self.province_combo["values"] = list(data.keys())

        self.province_combo.bind("<<ComboboxSelected>>", self.update_cities)

        self.province_combo.grid(row=0, column=1, padx=5, pady=5)

        

        # 城市级联菜单

        self.city_label = ttk.Label(self, text="选择城市:")

        self.city_label.grid(row=1, column=0, padx=5, pady=5)

        

        self.city_combo = ttk.Combobox(self, textvariable=self.city_var, state="readonly")

        self.city_combo.bind("<<ComboboxSelected>>", self.update_districts)

        self.city_combo.grid(row=1, column=1, padx=5, pady=5)

        

        # 地区级联菜单

        self.district_label = ttk.Label(self, text="选择地区:")

        self.district_label.grid(row=2, column=0, padx=5, pady=5)

        

        self.district_combo = ttk.Combobox(self, textvariable=self.district_var, state="readonly")

        self.district_combo.grid(row=2, column=1, padx=5, pady=5)

        

        # 显示选择结果按钮

        self.show_button = ttk.Button(self, text="显示选择结果", command=self.show_selection)

        self.show_button.grid(row=3, columnspan=2, padx=5, pady=5)

        

    def update_cities(self, event):

        province = self.province_var.get()

        cities = list(data.get(province, {}).keys())

        self.city_combo["values"] = cities

        self.city_var.set(cities[0] if cities else "")

        self.update_districts(None)

        

    def update_districts(self, event):

        province = self.province_var.get()

        city = self.city_var.get()

        districts = data.get(province, {}).get(city, [])

        self.district_combo["values"] = districts

        self.district_var.set(districts[0] if districts else "")

        

    def show_selection(self):

        province = self.province_var.get()

        city = self.city_var.get()

        district = self.district_var.get()

        selection_str = f"选择的地区:{province} - {city} - {district}"

        self.selection_label = ttk.Label(self, text=selection_str)

        self.selection_label.grid(row=4, columnspan=2, padx=5, pady=5)

if __name__ == "__main__":

    app = ChineseCascadeMenu()

    app.mainloop()
posted @ 2024-06-06 22:06  chrisrmas、  阅读(11)  评论(0编辑  收藏  举报