下拉菜单命令执行

时间:2018-05-22 10:10:39

标签: python-3.x tkinter drop-down-menu execution

是否可以使用python的tkinter模块获取下拉菜单,该模块在单击时执行命令?

# Imports tkinter
from tkinter import *

#creates a root, then a popup and a frame inside to work with
root = Tk()
popup = Toplevel()
popup_frame = Frame(popup)

#creates a labelframe to hold the label and menu
labelframe_example = LabelFrame(root, text="Title For Widget", pady=3, padx=3)
#label providing description about menu
Label(labelframe_example, text="Descriptive label", width=25, height=0).pack()
#options displayed in dropdown menu
options = ["Option1","Option2", "Option3"]
#gains the variable of what is in the menu 
variable = StringVar(labelframe_example)
#sets the inital text for the menu, before an option is selected
variable.set("Pre-option text placeholder") 
#places the dropdown menu in the labelframe, with the displayed text being whatever the variable is and calls the options, expands the menu to size of labelframe
OptionMenu(labelframe_example, variable, *options).pack(fill=BOTH, expand=1)
#packs frame
labelframe_example.pack()

#creates a function to destroy old frame inside the popup window and recreate it using the information for option 1
def executed_command_1(popup_frame):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    Label(popup_frame, text="Hey Look it worked, a new frame was created for option 1").pack()

#creates a function to destroy old frame inside the popup window and recreate it using the information for option 2
def executed_command_2(popup_frame):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    Label(popup_frame, text="Hey Look it worked, a new frame was created for option 2").pack()

#creates a function to destroy old frame inside the popup window and recreate it using the information for option 3
def executed_command_3(popup_frame):
    popup_frame.destroy()
    popup_frame = Frame(popup)
    Label(popup_frame, text="Hey Look it worked, a new frame was created for option 3").pack()


#unsure how to actually make a way for option specific function to open
option_execution_button = Button(root, text="Open Option", width=25, padx=5, pady=5,               
                         command=lambda:executed_command_1/2/3(popup_frame)).pack()

换句话说,有2个窗口,1个窗口有一个带有下拉菜单的窗口小部件,另一个窗口应该根据选择的任何选项进行更新,如果有办法,按钮实际上是不必要的发生没有按钮也会很好。

我无法找到方法,欢迎任何帮助。

很抱歉,如果您不明白,如果您不明白,我会尝试进一步澄清。

0 个答案:

没有答案