Home > Mobile >  Using Tkinter Drop-Down Menu choices as criteria for different commands
Using Tkinter Drop-Down Menu choices as criteria for different commands

Time:01-22

I have been assigned with creating a simple Python project of my choice and as for now I am working on a little Tkinter GUI app. Having successfully written the part of the code responsible for multiple Drop-Down Menus, I wonder how to use combination of users' chosen options for creating commands opening the designated window. Is there any neat way to do it ? Would it be enough to somehow define the combination of choices in the function, which constructs button command ? Thank a lot for any piece of advice on this.

#first drop-down menu
 options1 = [
    '............',
    '............',
    '............'
]
chosen1 = StringVar()
chosen1.set('............')

vyber1 = OptionMenu(whiteboard, chosen1, *options1)
vyber1.place(relx = 0.35, rely = 0.35, relwidth = 0.35, relheight = 0.05)

# second drop-down menu
options2 = [
    '............',
    '............',
    '............',
    '............'
]
chosen2 = StringVar()
chosen2.set('............')

vyber2 = OptionMenu(whiteboard, chosen2, *options2)
vyber2.place(relx = 0.35, rely = 0.46, relwidth = 0.35, relheight = 0.05)

#third drop-down menu
options3 = [
    '............',
    '............',
    '............',
    '............'
]
chosen3 = StringVar()
chosen3.set('............')

vyber3 = OptionMenu(whiteboard, chosen3, *options3)
vyber3.place(relx = 0.35, rely = 0.57, relwidth = 0.35, relheight = 0.05)

CodePudding user response:

A "neat" and orderly way to do it in Python would be to map each combination of choices to the desired window-opening function via a dictionary. Looking up the function to call would simply be a matter of combining the current choices together to form a dictionary key, then using that to look-up the corresponding function to call.

Below is a runnable example of doing that:

import tkinter as tk
from tkinter import messagebox


whiteboard = tk.Tk()
whiteboard.geometry('300x300')

#first drop-down menu
options1 = ['1', '2', '3']
chosen1 = tk.StringVar(value=options1[0])

vyber1 = tk.OptionMenu(whiteboard, chosen1, *options1)
vyber1.place(relx=0.35, rely=0.35, relwidth=0.35, relheight=0.10)

# second drop-down menu
options2 = ['A', 'B', 'C', 'D']
chosen2 = tk.StringVar(value=options2[0])

vyber2 = tk.OptionMenu(whiteboard, chosen2, *options2)
vyber2.place(relx=0.35, rely=0.46, relwidth=0.35, relheight=0.10)

#third drop-down menu
options3 = ['Red', 'Green', 'Blue', 'Yellow']
chosen3 = tk.StringVar(value=options3[0])

vyber3 = tk.OptionMenu(whiteboard, chosen3, *options3)
vyber3.place(relx=0.35, rely=0.57, relwidth=0.35, relheight=0.10)

# Window opening functions.
def open_window_1ARed():
    new_win = tk.Toplevel(whiteboard)
    new_win.geometry('300x100')
    new_win.title('1-A-Red window')
    tk.Label(new_win, text='Hello world').pack()

def open_window_1BBlue():
    new_win = tk.Toplevel(whiteboard)
    new_win.geometry('300x100')
    new_win.title('1-B-Blue window')
    tk.Label(new_win, text='Hello world').pack()

def display_error():
    messagebox.showerror('Sorry', "Unsupported combination!")

# Dictionary mapping combinations of choices to functions.
open_windows_commands = {
    '1ARed': open_window_1ARed,
    '1BBlue': open_window_1BBlue,
}

def open_window():
    key = f'{chosen1.get()}{chosen2.get()}{chosen3.get()}'  # Create key from choices.
    open_window_command = open_windows_commands.get(key, display_error)
    open_window_command()

btn = tk.Button(whiteboard, text='Open window', command=open_window)
btn.place(relx=0.35, rely=0.80)

whiteboard.mainloop()

  •  Tags:  
  • Related