Выполнение команды выпадающего меню - PullRequest
0 голосов
/ 22 мая 2018

Можно ли получить выпадающее меню с помощью модулей 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 окне есть виджет с выпадающим-Вниз меню, а другое должно обновляться в зависимости от того, какая опция была выбрана, кнопка на самом деле не нужна, если есть способ, чтобы это произошло без кнопки, которая тоже подойдет.

Я просто не могу найти способ сделать это, любая помощь приветствуется.

Извините, если это трудно понять, если вы не понимаете, я попытаюсь уточнить дальше.

...