# Buggy code
def create_window(
    title,
    url=None,
    js_api=None,
    width=800,
    height=600,
    resizable=True,
    fullscreen=False,
    min_size=(200, 100),
    strings={},
    confirm_quit=False,
    background_color="#FFFFFF",
    text_select=False,
    debug=False,
):
    """
    Create a web view window using a native GUI. The execution blocks after this function is invoked, so other
    program logic must be executed in a separate thread.
    :param title: Window title
    :param url: URL to load
    :param width: window width. Default is 800px
    :param height:window height. Default is 600px
    :param resizable True if window can be resized, False otherwise. Default is True
    :param fullscreen: True if start in fullscreen mode. Default is False
    :param min_size: a (width, height) tuple that specifies a minimum window size. Default is 200x100
    :param strings: a dictionary with localized strings
    :param confirm_quit: Display a quit confirmation dialog. Default is False
    :param background_color: Background color as a hex string that is displayed before the content of webview is loaded. Default is white.
    :param text_select: Allow text selection on page. Default is False.
    :return: The uid of the created window.
    """
    uid = "child_" + uuid4().hex[:8]

    valid_color = r"^#(?:[0-9a-fA-F]{3}){1,2}$"
    if not re.match(valid_color, background_color):
        raise ValueError(
            "{0} is not a valid hex triplet color".format(background_color)
        )

    if not _initialized:
        # Check if starting up from main thread; if not, wait; finally raise exception
        if current_thread().name != "MainThread":
            if not _webview_ready.wait(5):
                raise Exception(
                    "Call create_window from the main thread first, and then from subthreads"
                )
        else:
            _initialize_imports()
            localization.update(strings)
            uid = "master"

    _webview_ready.clear()  # Make API calls wait while the new window is created
    gui.create_window(
        uid,
        make_unicode(title),
        transform_url(url),
        width,
        height,
        resizable,
        fullscreen,
        min_size,
        confirm_quit,
        background_color,
        debug,
        js_api,
        text_select,
        _webview_ready,
    )

    return uid


# Traceback (KeyError)
Exception in thread Thread-3:
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Program Files\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "ui_pywebview.py", line 23, in create_second_window
second_window = webview.create_window("Window 2", os.path.abspath("web\\window2.html"), js_api=SecondWindowApi)
File "C:\Program Files\Python36\lib\site-packages\webview\__init__.py", line 183, in create_window
background_color, debug, js_api, text_select, _webview_ready)
File "C:\Program Files\Python36\lib\site-packages\webview\winforms.py", line 252, in create_window
BrowserView.instances['master'].Invoke(Func[Type](create))
KeyError: 'master'

# Fixed code
def create_window(
    title,
    url=None,
    js_api=None,
    width=800,
    height=600,
    resizable=True,
    fullscreen=False,
    min_size=(200, 100),
    strings={},
    confirm_quit=False,
    background_color="#FFFFFF",
    text_select=False,
    debug=False,
):
    """
    Create a web view window using a native GUI. The execution blocks after this function is invoked, so other
    program logic must be executed in a separate thread.
    :param title: Window title
    :param url: URL to load
    :param width: window width. Default is 800px
    :param height:window height. Default is 600px
    :param resizable True if window can be resized, False otherwise. Default is True
    :param fullscreen: True if start in fullscreen mode. Default is False
    :param min_size: a (width, height) tuple that specifies a minimum window size. Default is 200x100
    :param strings: a dictionary with localized strings
    :param confirm_quit: Display a quit confirmation dialog. Default is False
    :param background_color: Background color as a hex string that is displayed before the content of webview is loaded. Default is white.
    :param text_select: Allow text selection on page. Default is False.
    :return: The uid of the created window.
    """

    valid_color = r"^#(?:[0-9a-fA-F]{3}){1,2}$"
    if not re.match(valid_color, background_color):
        raise ValueError(
            "{0} is not a valid hex triplet color".format(background_color)
        )

    # Check if starting up from main thread; if not, wait; finally raise exception
    if current_thread().name == "MainThread":
        uid = "master"

        if not _initialized:
            _initialize_imports()
            localization.update(strings)
    else:
        uid = "child_" + uuid4().hex[:8]

        if not _webview_ready.wait(5):
            raise Exception(
                "Call create_window from the main thread first, and then from subthreads"
            )

    _webview_ready.clear()  # Make API calls wait while the new window is created
    gui.create_window(
        uid,
        make_unicode(title),
        transform_url(url),
        width,
        height,
        resizable,
        fullscreen,
        min_size,
        confirm_quit,
        background_color,
        debug,
        js_api,
        text_select,
        _webview_ready,
    )

    return uid