代码
from tkinter import *
from tkinter import ttk
select = {"年度": ("第一季度", "第二季度", "第三季度", "第四季度"),
"第一季度": ("1月", "2月", "3月"),
"第二季度": ("4月", "5月", "6月"),
"第三季度": ("8月", "7月", "9月"),
"第四季度": ("10月", "11月", "12月")}
class Perf_GUI():
def __init__(self, init_window_name):
self.init_window_name = init_window_name
def init_window(self):
self.init_window_name.title("二级菜单联动") # 设置窗口名字
self.init_window_name.geometry('770x520+100+50') # 设置窗口大小
self.plan_period_label = Label(self.init_window_name, text="一级菜单:")
self.plan_period_label.grid(row=1, column=0)
xVariable = StringVar()
self.plan_ttk = ttk.Combobox(self.init_window_name, textvariable=xVariable)
self.plan_ttk.grid(row=1, column=1)
self.plan_ttk['value'] = list(select.keys())
self.report_period_label = Label(self.init_window_name, text="二级菜单:")
self.report_period_label.grid(row=1, column=2)
der = StringVar()
self.report_ttk = ttk.Combobox(self.init_window_name, textvariable=der)
self.report_ttk.grid(row=1, column=3)
# Combobox下拉菜单实现联动
def xFunc(event):
self.report_ttk.delete(0, END)
value = xVariable.get()
self.report_ttk['value'] = select[value]
self.report_ttk.current(0) # 设置默认值
self.plan_ttk.bind("<<ComboboxSelected>>", xFunc)
def gui_start():
init_window = Tk()
Gui_Tool = Perf_GUI(init_window)
Gui_Tool.init_window()
init_window.mainloop()
if __name__ == '__main__':
gui_start()
效果图
一级菜单选择年度时:
一级菜单选择第一季度时: