首页 > 解决方案 > 基于 OptionMenu 选择改变 Tkinter 按钮的命令功能

问题描述

我在 python 中使用 Tkinter,并且有一个带有各种选项和一个按钮的下拉菜单。我希望这个按钮的命令功能根据下拉菜单的选择而改变。但是,我对选择变量的初始设置似乎没有改变。我该如何解决这个问题?目前,似乎我的初始设置clicked导致按钮以一种方式配置,并且不会根据用户选择而改变。

def get_chosen_opp(clicked):
    return clicked.get()

clicked=StringVar() #this will be the chosen team
drop=OptionMenu(self,clicked,"Catholic","Coast Guard","Maine Maritime","Merchant Marine","Norwich","Springfield","WPI","Non-Conference Opponent")
clicked.set("Non-Conference Opponent")
drop.place(relx=.5,rely=.35,anchor='n',relheight=.1,relwidth=.4)

lower_frame=tk.Frame(self,bd=5,bg='#252122')  
lower_frame.place(relx=.5,rely=.6,relwidth=0.35,relheight=.1,anchor='n')
continue_button=tk.Button(lower_frame,text='See Chart',font=30,bg='#7F120E')

#set custom stats if non-conference oponent
selection=get_chosen_opp(clicked)
if selection=="Non-Conference Opponent":
    continue_button.config(command=lambda:controller.show_label(set_nc_stats))
else:
    continue_button.config(command=lambda:controller.show_label(show_chart))

continue_button.place(relheight=1,relwidth=1)

标签: pythontkinter

解决方案


推荐阅读