cef python - 调整大小时冻结

时间:2017-04-04 13:58:35

标签: python interface freeze chromium-embedded

今天我正在尝试使用cefpython开发一个UI,它允许我嵌入一个Web浏览器并使用javascript绑定与它进行交互。

我用它在Windows平台上开发。 为此,我正在使用" multi_threaded_message_loop"旗帜让我获得了表现。 我也在python 3上使用wxpython来嵌入它。

问题是当我调整窗口大小时,使用 WindowUtils.OnSize()会冻结我的应用程序。 99%的情况下,它会在浏览器加载时发生(但也会在完成时很少发生)。

以下是重现的示例代码:

import platform
import sys

import wx
from cefpython3 import cefpython

WindowUtils = cefpython.WindowUtils()

WIDTH = 800
HEIGHT = 600

import os

class MainFrame(wx.Frame):
    browser = None
    mainPanel = None

    def createMainBrowser(self):
        self.browser = self.createBrowser(self.mainPanel)

    def createBrowser(self, parent):
        browser = cefpython.CreateBrowserSync(
            self.getWindowInfo(parent),
            browserSettings={},
            navigateUrl='http://www.google.com'
        )
        return browser

    def getWindowInfo(self, parent):
        windowInfo = cefpython.WindowInfo()
        windowInfo.SetAsChild(parent.GetHandle(), [0, 0, WIDTH, HEIGHT])
        return windowInfo

    def __init__(self):
        wx.Frame.__init__(
            self, parent=None, id=wx.ID_ANY, title='wx', size=(WIDTH, HEIGHT)
        )
        self.mainPanel = wx.Panel(self)
        self.mainPanel.SetBackgroundColour(wx.GREEN)

        cefpython.PostTask(cefpython.TID_UI, self.createMainBrowser)

        self.mainPanel.Bind(wx.EVT_SIZE, self.OnSize)

    def OnSize(self, _):
        if not self.browser:
            return

        WindowUtils.OnSize(self.mainPanel.GetHandle(), 0, 0, 0)
        self.browser.NotifyMoveOrResizeStarted()


class App(wx.App):
    def OnInit(self):
        frame = MainFrame()
        frame.Show()
        return True

if __name__ == '__main__':
    sys.excepthook = cefpython.ExceptHook  # To shutdown all CEF processes on error
    cefpython.Initialize({
        "locales_dir_path": cefpython.GetModuleDirectory() + "/locales",
        "browser_subprocess_path": cefpython.GetModuleDirectory() + "/subprocess",
        "auto_zooming": "system_dpi",
        "multi_threaded_message_loop": True,
    })
    app = App(False)
    app.MainLoop()
    cefpython.Shutdown()

非常感谢你的帮助!

Alann

1 个答案:

答案 0 :(得分:1)

问题解决了! 而不是使用

<button class="bttnrolldice" onclick="diceRoll()">Roll Dice</button><h1 id="rollresults"></h1> 
<script>
    function diceRoll() {
            var die1 =  math.ceil(math.random() * 7;
            var die2 =  math.ceil(math.random() * 7;
            document.getElementById("rollresults").innerHTML = (die1 + die2); }
</script>

我用

def OnSize(self, _):
    if not self.browser:
        return

    WindowUtils.OnSize(self.mainPanel.GetHandle(), 0, 0, 0)
    self.browser.NotifyMoveOrResizeStarted()

我不知道这是因为我是在Windows 10上,但是WindowsUtils可能需要更新!