Not able to run this code. After two levels, the code seems to fail, in retrieving the values required to run the next level

Question:

I have made a GUI for selecting different floors. The user has to mention the plot, after which the output is shown for the levels/clusters and then in the next level, an option is given to select the level, and in the next, the option to select the room. But after finishing two level, i.e., after selecting the plot and the cluster, the code seems to fail, it is not giving the selection criteria for the floor.

Here’s my code:

import tkinter as tk

class App:
    def __init__(self, master):
        self.master = master
        self.master.geometry("300x200")
        self.master.title("Document Finder")

        # Create dropdown menus
        self.location_var = tk.StringVar()
        self.location_var.set("Select the Plot")
        self.location_dropdown = tk.OptionMenu(self.master, self.location_var, "Plot A", "Plot B",command = self.update_sub_area_menu)
        self.location_dropdown.pack()
        
        self.sub_area_var = tk.StringVar()
        self.sub_area_var.set("Select the sub-area")
        self.sub_area_dropdown = tk.OptionMenu(self.master, self.sub_area_var,"",command = self.update_level_menu)
        self.sub_area_dropdown.pack()

        self.level_var = tk.StringVar()
        self.level_var.set("Select a level")
        self.level_dropdown = tk.OptionMenu(self.master, self.level_var,"",command=self.update_room_menu)
        self.level_dropdown.pack()

        self.room_var = tk.StringVar()
        self.room_var.set("Select a room")
        self.room_dropdown = tk.OptionMenu(self.master, self.room_var,"")
        self.room_dropdown.pack()

        # Create button to search for document
        self.search_button = tk.Button(self.master, text="Submit")
        self.search_button.pack()

    def update_sub_area_menu(self,selection):
        if selection == "Plot A":
            sub_areas = ["Basement A", "Club Lounge", "Hotel-Luxury", "Hotel-Service Apartments", "Hotel-Ultra Luxury", "Link Levels", "Others", "Penthouses", "Podium A", "Rental Offices", "Restaurants", "Spa/Gym"]
        elif selection == "Plot B":
            sub_areas = ["Apartments-Duplex", "Apartments-Simplex", "Basement B", "Link", "Mech & Roof", "Podium B"]
        else:
            sub_areas = []
        self.sub_area_var.set("Select the sub-area")
        self.sub_area_dropdown['menu'].delete(0, 'end')
        for sub_area in sub_areas:
            self.sub_area_dropdown['menu'].add_command(label=sub_area, command=tk._setit(self.sub_area_var, sub_area))
    
    def update_level_menu(self, selection):
        if selection == "Basement A":
            levels = ["B1", "B2", "B3", "B4", "B5", "B6", "B7"]
        elif selection == "Club Lounge":
            levels = ["53F"]
        elif selection == "Hotel-Luxury":
            levels = ["30F", "31F", "32F", "33F", "34F", "35F", "36F", "37F", "38F", "39F", "40F", "41F", "42F", "43F", "44F"]
        elif selection == "Hotel-Service Apartments":
            levels = ["54F", "55F", "56F", "57F", "58F", "59F", "60F"]
        elif selection == "Hotel-Ultra Luxury":
            levels = ["46F", "47F", "48F", "49F","50F", "51F", "52"]
        elif selection == "Link Levels":
            levels = ["23F", "24F", "25F", "26F"]
        elif selection == "Others":
            levels = ["22F", "23F", "29F", "45F", "67F", "RF", "RF"]
        elif selection == "Penthouses":
            levels = ["61F", "62F", "63F", "64F", "65F", "66F"]
        elif selection == "Podium A":
            levels = ['GF', "1F", "2F","3F"]
        elif selection == "Rental Offices":
            levels = ["5F", "6F", "7F", "8F", "9F", "10F", "11F", "12F", "13F", "14F", "15F","16F","17F","18F","19F","20F","21F"]
        elif selection == "Restaurants":
            levels = ["4F"]
        elif selection == "Spa/Gym":
            levels = ["27F", "28F"]
        elif selection == "Apartments-Duplex":
            levels = ["30F", "31F", "32F", "33F", "34F", "35F", "36F", "37F", "38F", "39F", "40F", "41F", "42F", "43F", "44F", "45", "46F", "47F", "48F", "49F","50F", "51F", "52F", "53F", "54F","55F", "56F","57F","58F"]
        elif selection == "Apartments-Simplex":
            levels = ["5F", "6F", "7F", "8F", "9F","10F","11F","12F","13F","14F","15F","16F","17F","18F","19F","20F","21F","22F","23F","24F","25F","26F"]
        elif selection == "Basement B":
            levels = ["B1","B2","B3","B4","B5","B6","B7"]
        elif selection == "Link":
            levels = ["28F", "29F"]
        elif selection == "Mech & Roof":
            levels = ["RF"]
        elif selection == "Podium B":
            levels = ["GF","1F","2F","4F"]
        else:
            levels = []
        
        self.level_dropdown['menu'].delete(0, 'end')
        for level in levels:
            self.level_dropdown['menu'].add_command(label=level, command=tk._setit(self.level_var, level))
    

    def update_room_menu(self, selection):
        if selection == "B1":
            rooms = ["FWT BA 001","FWT BA 002","RAMP 1 BA 003","MECH WATER TANK (WATER FEATURE 13) BA 004", 
                     ]
        else:
            rooms = []
        self.room_var.set("Select the room")
        self.room_dropdown['menu'].delete(0, 'end')
        for room in rooms:
            self.room_dropdown['menu'].add_command(label=room, command=tk._setit(self.room_var, room))

root = tk.Tk()
app = App(root)
root.mainloop()

I’m using Python 3.

Asked By: hexapod

||

Answers:

You forget to pass the required function to _setit(...):

...
class App:
    def __init__(self, master):
        ...

    def update_sub_area_menu(self, selection):
        ...
        for sub_area in sub_areas:
            self.sub_area_dropdown['menu'].add_command(label=sub_area,
                command=tk._setit(self.sub_area_var, sub_area, self.update_level_menu))

    def update_level_menu(self, selection):
        ...
        for level in levels:
            self.level_dropdown['menu'].add_command(label=level,
                command=tk._setit(self.level_var, level, self.update_room_menu))

    ...
Answered By: acw1668