Module Auto-GUI.auto_components.drop_down_menu

Expand source code
import tkinter
from tkinter import OptionMenu, StringVar

from miscellaneous.important_variables import WINDOW


class DropDownMenu(OptionMenu):
    """Extends the tkinter's class OptionMenu and it shows a list of possible values once it is clicked. It adds the
        functionality of setting the selected value of the OptionMenu programmatically, which the regular OptionMenu does not have"""

    selected_item = None
    command = lambda unused: ""  # By default the command should do nothing when it is called
    command_args = []

    # The function and function_args should do nothing by default
    def __init__(self, master, current_item_index, items):
        """Initializes the object"""

        self.selected_item = StringVar()

        # The items other than the currently selected item
        other_items = items[:current_item_index] + items[current_item_index + 1:]

        super().__init__(master, self.selected_item, items[current_item_index], *other_items, command=lambda unused: self.handle_click())
        self.set_selected_item(items[current_item_index])

    def get_selected_item(self):
        """
            Returns:
                str: the item that is currently selected for the DropDownMenu"""

        return self.selected_item.get()

    def set_selected_item(self, value):
        """Sets the item that is currently selected for the DropDownMenu"""

        self.selected_item.set(value)

    def set_command(self, command, command_args):
        """Sets the function that is called when the DropDownMenu is clicked"""

        self.command, self.command_args = command, command_args

    def handle_click(self):
        """Calls the function 'command' when the DropDownMenu is clicked"""

        if len(self.command_args) != 0:
            self.command(*self.command_args)

        else:
            self.command()

    def focus_force(self) -> None:
        """Forces the mouse to focus on the DropDownMenu and for it to be opened"""

        pass

Classes

class DropDownMenu (master, current_item_index, items)

Extends the tkinter's class OptionMenu and it shows a list of possible values once it is clicked. It adds the functionality of setting the selected value of the OptionMenu programmatically, which the regular OptionMenu does not have

Initializes the object

Expand source code
class DropDownMenu(OptionMenu):
    """Extends the tkinter's class OptionMenu and it shows a list of possible values once it is clicked. It adds the
        functionality of setting the selected value of the OptionMenu programmatically, which the regular OptionMenu does not have"""

    selected_item = None
    command = lambda unused: ""  # By default the command should do nothing when it is called
    command_args = []

    # The function and function_args should do nothing by default
    def __init__(self, master, current_item_index, items):
        """Initializes the object"""

        self.selected_item = StringVar()

        # The items other than the currently selected item
        other_items = items[:current_item_index] + items[current_item_index + 1:]

        super().__init__(master, self.selected_item, items[current_item_index], *other_items, command=lambda unused: self.handle_click())
        self.set_selected_item(items[current_item_index])

    def get_selected_item(self):
        """
            Returns:
                str: the item that is currently selected for the DropDownMenu"""

        return self.selected_item.get()

    def set_selected_item(self, value):
        """Sets the item that is currently selected for the DropDownMenu"""

        self.selected_item.set(value)

    def set_command(self, command, command_args):
        """Sets the function that is called when the DropDownMenu is clicked"""

        self.command, self.command_args = command, command_args

    def handle_click(self):
        """Calls the function 'command' when the DropDownMenu is clicked"""

        if len(self.command_args) != 0:
            self.command(*self.command_args)

        else:
            self.command()

    def focus_force(self) -> None:
        """Forces the mouse to focus on the DropDownMenu and for it to be opened"""

        pass

Ancestors

  • tkinter.OptionMenu
  • tkinter.Menubutton
  • tkinter.Widget
  • tkinter.BaseWidget
  • tkinter.Misc
  • tkinter.Pack
  • tkinter.Place
  • tkinter.Grid

Class variables

var command_args
var selected_item

Methods

def command(unused)
Expand source code
command = lambda unused: ""  # By default the command should do nothing when it is called
def focus_force(self) ‑> None

Forces the mouse to focus on the DropDownMenu and for it to be opened

Expand source code
def focus_force(self) -> None:
    """Forces the mouse to focus on the DropDownMenu and for it to be opened"""

    pass
def get_selected_item(self)

Returns

str
the item that is currently selected for the DropDownMenu
Expand source code
def get_selected_item(self):
    """
        Returns:
            str: the item that is currently selected for the DropDownMenu"""

    return self.selected_item.get()
def handle_click(self)

Calls the function 'command' when the DropDownMenu is clicked

Expand source code
def handle_click(self):
    """Calls the function 'command' when the DropDownMenu is clicked"""

    if len(self.command_args) != 0:
        self.command(*self.command_args)

    else:
        self.command()
def set_command(self, command, command_args)

Sets the function that is called when the DropDownMenu is clicked

Expand source code
def set_command(self, command, command_args):
    """Sets the function that is called when the DropDownMenu is clicked"""

    self.command, self.command_args = command, command_args
def set_selected_item(self, value)

Sets the item that is currently selected for the DropDownMenu

Expand source code
def set_selected_item(self, value):
    """Sets the item that is currently selected for the DropDownMenu"""

    self.selected_item.set(value)