无法设置键盘焦点

时间:2019-12-27 11:54:03

标签: tkinter tkinter-canvas pysimplegui cefpython

我正在尝试在Windows上基于PySimpleGUI的应用程序中使用cefpython3(以使用babylon.js来显示3d框架)。

集成有效,但是打开浏览器窗口后,我无法在PySimpleGUI Spin元素中使用键盘(我可以使用向上/向下箭头和鼠标来修改值)。

我使用以下代码打开浏览器:

import threading
import sys

import win32gui
import PySimpleGUI as sg
from cefpython3 import cefpython as cef

browserOpened = False

def openBrowser(window):
    if not browserOpened:
        sys.excepthook = cef.ExceptHook
        settings={"multi_threaded_message_loop": True}
        cef.Initialize(settings)
        windowInfo = cef.WindowInfo()
        rect = [0, 0, 1000, 500]
        windowHandle = window.FindElement("browserFrame").TKCanvas.winfo_id()
        windowInfo.SetAsChild(windowHandle, rect)
        cef.PostTask(cef.TID_UI, createBrowser, windowInfo, {}, "file:///..../index.html")
        browserOpened = True

def WebUiThread():
    win32gui.PumpMessages()

def createBrowser(windowInfo, settings, url):
    browser = cef.CreateBrowserSync(windowInfo,
                                    settings=settings,
                                    url=url)

def main(): 
    webUiThread=threading.Thread(target=WebUiThread)
    webUiThread.daemon=True
    webUiThread.start()

    layout=[
        [sg.Canvas(size=(1000, 500), key="browserFrame")],
        [sg.Button("Open Browser", key="openBrowser")],
        [sg.Spin(values=list(range(1, 100)), key="spinValue", enable_events=True)]
        ]
    window=sg.Window("Test Window", layout, return_keyboard_events=True, resizable=True)

    window.Finalize()

    while True:
        event, values = window.Read(timeout=100)

        if event==sg.TIMEOUT_KEY:
            continue

        print(event, value)

        if event is None:
            break

        elif event=="openBrowser":
            openBrowser(window)

    window.Close()    
    cef.Shutdown()

if __name__=="__main__":
    main()


0 个答案:

没有答案