简体   繁体   中英

Tkinter command doesnt reacts in second OptionMenu

I have 2 OptionMenus. The first one is to select Manufacturer and method assign to command clear options in second OptionMenu and add options assign to the selected manufacturer. The problem is that second OptionMenu dont trigger the method assign to it's command argument. When I delete/comment first OptionMenu then the second one work correctly. Why?
Here is piece of code:

        self.cells_menu = tk.OptionMenu(self.local_master,
                                        self.selected_cell,
                                        *[c.full_name() 
                                          for c in available_cells.values()
                                          if len(c.performed_tests)>0],
                                        command=lambda x: self.on_cell_select(x))
        self.cells_menu.config(width=40, anchor="w")
        self.cells_menu.grid(row=1, column=0, sticky="W", columnspan=2)
    # (...)
    self.selected_test = tk.StringVar(self.local_master)
        self.test_menu = tk.OptionMenu(self.local_master,
                                       self.selected_test,
                                       *(None, None),
                                       command=lambda x: print(self.selected_test, x))
        self.test_menu.config(width=40, anchor="w")
        self.test_menu.grid(row=3, column=0, sticky="W", columnspan=2)
    # (...)
     def on_cell_select(self, cell_name: str):
        self.selected_cell.set(cell_name)
        new_cell = self.available_cells[cell_name]
        self.update_test_list(new_cell,
                              self.test_menu,
                              self.selected_test)
    
    def on_test_select(self, cell_name: str):
        print(cell_name)
        curr_cell = self.available_cells[cell_name]
        print(curr_cell)
        

    def update_test_list(self,
                         new_cell: Cell,
                         menu_to_update: tk.OptionMenu,
                         assign_varstring: tk.StringVar):
        self.selected_test.set('')
        menu_to_update["menu"].delete(0, "end")
        
        for test in new_cell.performed_tests:
            menu_to_update["menu"].add_command(label=test,
                                               command=tk._setit(assign_varstring,
                                                                 test))```

I found the answer.
I just need to add third argument to add_command method. Just like this

def on_cell_select(self, cell_name: str):
    self.selected_cell.set(cell_name)
    new_cell = self.available_cells[cell_name]
    self.update_test_list(new_cell,
                          self.test_menu,
                          self.selected_test,
                          (...))
# (...)

def update_test_list(self,
                     new_cell: Cell,
                     menu_to_update: tk.OptionMenu,
                     assign_varstring: tk.StringVar,
                     command: Callable):
    self.selected_test.set('')
    menu_to_update["menu"].delete(0, "end")
    menu_to_update.config()
    for test in new_cell.performed_tests:
        menu_to_update["menu"].add_command(label=test,
                                           command=tk._setit(assign_varstring,
                                                             test,
                                                             command))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM