Exemple #1
0
    def test_basic_hook(self):
        self.expected = {"random_kwarg": "Hello World"}
        options = {"random_kwarg": "Default Value"}
        hook_ttk_widgets(self.basic_updater, options)
        button = ttk.Button(random_kwarg="Hello World")
        self.assertTrue(self.has_been_updated())

        self.assertTrue(is_hooked(options))
        self.assertTrue(hasattr(ttk.Button, generate_hook_name(options)))
        self.assertTrue("random_kwarg" in button.keys())
Exemple #2
0
    def test_multi_hooks(self):
        options1 = {"hook1": "Default One"}
        options2 = {"hook2": "Default Two"}
        self.expected = {"hook1": "Custom One"}
        self.second_expected = {"hook2": "Default Two"}

        name = hook_ttk_widgets(self.basic_updater, options1)
        hook_ttk_widgets(self.second_updater, options2)
        self.assertEquals(name, generate_hook_name(options1))

        self.assertTrue(is_hooked(options1))
        self.assertTrue(is_hooked(options2))

        button = ttk.Button(hook1="Custom One")

        self.assertTrue(is_hooked(options1))
        self.assertTrue(is_hooked(options2))

        self.assertTrue(self.has_been_updated())
        self.assertTrue(self.has_been_second_updated())

        self.assertTrue("hook1" in button.keys() and "hook2" in button.keys())
Exemple #3
0
    def test_multi_option_hooks_cget_config_keys_overwrite(self):
        options = {"hookx": "Default X", "hooky": "Default Y"}
        self.expected = {"hookx": "Default X", "hooky": "Option Y"}

        hook_ttk_widgets(self.basic_updater, options)
        self.assertTrue(is_hooked(options))
        self.assertTrue(is_hooked({"hookx": None}))
        self.assertTrue(is_hooked({"hooky": None}))

        button = ttk.Button(hooky="Option Y")

        self.assertTrue(self.has_been_updated())
        self.assertTrue("hooky" in button.keys())
        self.assertTrue("hookx" in button.keys())
        self.assertEqual("Default X", button.cget("hookx"))
        self.assertEqual("Option Y", button.cget("hooky"))

        self.expected = {"hookx": "Option X"}
        button.configure(hookx="Option X", command=self.window.destroy)
        self.assertTrue(self.has_been_updated())

        self.assertEqual("Option X", button.cget("hookx"))
        self.assertEqual("Option Y", button.cget("hooky"))
        self.assertIsNotNone(button.cget("command"))

        self.assertRaises(
            RuntimeError,
            lambda: hook_ttk_widgets(self.basic_updater, options))
        self.assertRaises(
            RuntimeError,
            lambda: hook_ttk_widgets(None, {"hookx": "New Default X"}))

        options["hookx"] = "New Default X"
        hook_ttk_widgets(None, options)
        self.expected = options.copy()
        ttk.Button()
        self.assertTrue(self.has_been_updated())
Exemple #4
0
    def test_user_hook_and_defaults(self):
        self.expected = {"not_user": "******"}
        options = self.expected.copy()
        hook_ttk_widgets(self.basic_updater, self.expected.copy())

        button_init = ttk.Button.__init__

        def __init__(self_widget, *args, **kwargs):
            self.user_hook_called = True
            button_init(self_widget, *args, **kwargs)

        ttk.Button.__init__ = __init__

        ttk.Button()
        self.assertTrue(self.user_hook_called)
        self.assertTrue(is_hooked(options))
        self.assertTrue(self.has_been_updated())
Exemple #5
0
 def test_hook_exists(self):
     self.assertTrue(is_hooked(tooltips.OPTIONS))
Exemple #6
0
        # Create a new tooltip
        options = OPTIONS["tooltip_options"].copy()
        options.update(getattr(holder, "tooltip_options", {}))
        options["text"] = tooltip
        tooltip_widget = Tooltip(self, **options)
    elif tooltip_widget is not None and tooltip is None:
        # Destroy existing tooltip
        tooltip_widget.destroy()
        tooltip_widget = None
    elif tooltip_widget is not None:
        # Update existing tooltip
        tooltip_widget.configure(text=tooltip)
    else:  # tooltip_widget is None and tooltip is None
        pass
    setattr(holder, "tooltip_widget", tooltip_widget)


def tooltip_options_updater(self, holder, tooltip_widget, options):
    """Update the options of a tooltip widget held on a widget"""
    # Update options for when a new tooltip is created
    new_options = getattr(holder, "tooltip_options", {})
    new_options.update(options)
    setattr(holder, "tooltip_options", new_options)
    if tooltip_widget is not None:
        # Tooltip already exists, configure it with new options
        tooltip_widget.configure(**new_options)


if not is_hooked(OPTIONS):
    hook_ttk_widgets(tooltip_options_hook, OPTIONS)