예제 #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())
예제 #2
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())
예제 #3
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())
예제 #4
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())
예제 #5
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)
예제 #6
0
# -*- coding: utf-8 -*-

# Copyright (c) RedFantom 2021
# For license see LICENSE
from tkinter import ttk
import tkinter as tk
from ttkwidgets.hook import hook_ttk_widgets

if __name__ == '__main__':
    hook_ttk_widgets(lambda s, o, v: print(s, o, v),
                     {"tooltip": "Default Value"})
    hook_ttk_widgets(lambda s, o, v: print(s, o, v),
                     {"hello_world": "second_hook"})

    original_init = ttk.Button.__init__

    def __init__(self, *args, **kwargs):
        print("User custom hook")
        original_init(self, *args, **kwargs)

    ttk.Button.__init__ = __init__

    window = tk.Tk()
    button = ttk.Button(window,
                        text="Destroy",
                        command=window.destroy,
                        tooltip="Destroys Window")
    button.pack()
    print([name for name in dir(button) if name.startswith("WidgetHook")])
    window.after(
        1000, lambda: button.configure(tooltip="Does not destroy window",