1

I'm working on a GUI-App with tkinter and customtkinter. When an option of an OptionMenu is selected, i want to trigger a function located in another class with keyword arguments. As far as i know, commands dont accept functions with arguments, so i want to use a functool.partial wrapper.

The relevant parts of code are the following:

class OptionsFrame(ctk.CTkFrame):
    def __init__(self, master,main):
        #creating optionmenu
        self.opt_window_func=ctk.CTkOptionMenu(self,values=self.values_window_func)

        #defining wrapped command call
        command_opt_windows_func=partial(self.main.command_opt
                                         ,opt_name="select_window_func"
                                         ,opt_value=self.opt_window_func.get())
        #setting command call
        self.opt_window_func.configure(command=command_opt_windows_func)
        self.opt_window_func.grid(row=1,column=1, padx=5, pady=5, sticky='new')


class MainClass():
    def __init__(self):
        self.app=App(main=self)
        self.app.mainloop() 

    def command_opt(self,opt_value,opt_name):
        self.set_param(opt_name,opt_value)
        self.update_opt()

Compiling and App start is working fine. When I trigger the command (by clicking on the OptionsMenu) I'll receive a TypeError:

tkinter\windows\widgets\ctk_optionmenu.py", line 381, in _dropdown_callback self._command(self._current_value) TypeError: command_opt() got multiple values for argument 'opt_value'

How should i call the function correctly with all given arguments?

peter
  • 11
  • 1
  • The problem is that there is choice passed to this callback, so after using partial the call is done as `self.main.command_opt(choice, opt_name='...', opt_value=...)`. – maciek97x Jul 07 '23 at 08:10
  • Maybe link to [this post](https://stackoverflow.com/questions/16626789/functools-partial-on-class-method), since command_opt is a method of a class. – Jon99 Jul 07 '23 at 08:35

0 Answers0