creat menue boxes
python
15 days ago
2.1 kB
9
Indexable
Never
import tkinter as tk def on_menu_select(): global second_dropdown_menu selected_option = var.get() print(f"Selected option: {selected_option}") # Create and populate the second dropdown based on the selected option in the first dropdown if selected_option == "City 1": second_options = ["Distric A", "District B", "District C"] elif selected_option == "City 2": second_options = ["Distric X", "District Y", "District Z"] else: second_options = [] # No options for other selections # Clear the current second dropdown second_dropdown.set("") # Clear the selected value second_dropdown_menu.destroy() # Destroy the existing second dropdown # Create a new second dropdown menu second_dropdown_menu = tk.OptionMenu(second_dropdown_window, second_dropdown, "", *second_options) second_dropdown_menu.pack() # Create the main window root = tk.Tk() root.title("Dropdown Menu Example") # Set the window size root.geometry("400x200") # Width x Height # Create a variable to hold the selected option in the first dropdown var = tk.StringVar() # Create a label for the first dropdown label1 = tk.Label(root, text="Select an option:") label1.pack() # Create the first dropdown menu options1 = ["Option 1", "Option 2", "Option 3"] dropdown1 = tk.OptionMenu(root, var, *options1) dropdown1.pack() # Create a button to trigger an action when the menu option is selected button = tk.Button(root, text="Select", command=on_menu_select) button.pack() # Create a Toplevel window for the second dropdown second_dropdown_window = tk.Toplevel(root) second_dropdown_window.title("Second Dropdown") # Set the window size second_dropdown_window.geometry("400x200") # Width x Height # Create a variable to hold the selected option in the second dropdown second_dropdown = tk.StringVar() # Create the second dropdown menu second_dropdown_menu = tk.OptionMenu(second_dropdown_window, second_dropdown, "") second_dropdown_menu.pack() # Run the main loop root.mainloop()